Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert table in pandas
#1
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
Reply
#2
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()
nilamo and tgottsc1 like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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

Best,

Tim
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert MultiLayer XML to DataFrame using Pandas vsingh17 0 40 Yesterday, 03:50 PM
Last Post: vsingh17
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 932 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 665 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 515 Apr-29-2020, 06:45 PM
Last Post: nisusavi
  Convert XML into DB table Rohit 3 761 Jan-14-2020, 04:44 AM
Last Post: Rohit
  MYSQL how to assign a table for each user in my username table database? YoshikageKira 1 1,001 Dec-26-2019, 05:57 AM
Last Post: buran
  Python convert csv to json with nested array without pandas terrydidi 2 4,868 Jan-12-2019, 02:25 AM
Last Post: terrydidi
  convert text file data to HTML table in python Tirumal 5 7,892 Dec-29-2017, 04:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020