Python Forum

Full Version: Convert table in pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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()
Many thanks buran, that's very helpful to know!

Best,

Tim