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()
tgottsc1 and nilamo 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
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,323 Oct-06-2023, 04:32 PM
Last Post: cubangt
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 704 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Using pyodbc&pandas to load a Table data to df tester_V 3 746 Sep-09-2023, 08:55 PM
Last Post: tester_V
  python pandas sql table with header mg24 3 1,854 Dec-08-2022, 08:31 PM
Last Post: Larz60+
  Convert Json to table format python_student 2 5,051 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert .xlsx to Format as Table bnadir55 0 860 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,541 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,224 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Convert MultiLayer XML to DataFrame using Pandas vsingh17 0 2,020 Apr-14-2021, 03:50 PM
Last Post: vsingh17
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,557 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt

Forum Jump:

User Panel Messages

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