Python Forum
Convert table in pandas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert table in pandas (/thread-32182.html)



Convert table in pandas - tgottsc1 - Jan-26-2021

Hi guys,

I have a very basic question about how to convert the design of a table. I would highly appreciate if someone could please help me.

I have created the following table as a pandas dataframe:

Output:
element measure periodType periodDescription value element A Performance Calendar Month 1m 0.05 element A Performance Year 1y 0.10 element A Performance Year 3y 0.15
I would like to bring this table into the following format:

Output:
Performance 1m 1y 3y element A 0.05 0.10 0.15
I know how to run basic merge commands etc., but I have no idea how to approach this problem.

Thank you so much for any help!

Best,

Tim


RE: Convert table in pandas - buran - Jan-26-2021

DataFrame.pivot()
print(df)
print(df.pivot(index='element', columns='periodDescription', values='value')
Output:
element measure periodType periodDescription value 0 element A Performance Calendar Month 1m 0.05 1 element A Performance Year 1y 0.10 2 element A Performance Year 3y 0.15 periodDescription 1m 1y 3y element element A 0.05 0.1 0.15
you can also look at pandas.pivot_table() and DataFrame.pivot_table()


RE: Convert table in pandas - tgottsc1 - Jan-26-2021

Many thanks buran, that's very helpful to know!

Best,

Tim