0

I was looking for a while but i didn't find a solution for my problem ...

I have a csv with the following structure:

date_time, country, temp, dc
2018-01-01 00:00:00, Germany, 12, 0
...
2018-01-01 00:00:00, Austria, 13, 3
...
2018-01-01 00:00:00, France, 4, 9
...

as you can see, date_time will repeat.

I want to use python pandas to get the following structure:

|                     | Germany  | Austria  |  France
|                     | temp, dc | temp, dc | temp, dc
________________________________________________________
| 2018-01-01 00:00:00 | 12  , 0  | 13  , 3  | 4   , 9

I want to get two headers .. first separates the countries, second the attributes temp and dc. My index should be the date_time attribute.

Thank you for help!!!

smci
  • 32,567
  • 20
  • 113
  • 146
khuesmann
  • 188
  • 1
  • 12

2 Answers2

3

This will give you what you want:

df.pivot_table(index='date_time', columns='country', values=['temp', 'dc']).swaplevel(axis=1).sort_index(axis=1)
#country   Austria      France      Germany     
#               dc temp     dc temp      dc temp
#date_time                                      
#1               3   13      9    4       0   12
zipa
  • 27,316
  • 6
  • 40
  • 58
0

try this,

df =df.groupby('date_time').apply(lambda x:x.set_index(['date_time','country']).unstack()).swaplevel(axis=1).reset_index(level=1, drop=True)

Output:

country             Austria France Germany Austria France Germany
                       temp   temp    temp      dc     dc      dc
date_time                                                        
2018-01-01 00:00:00      13      4      12       3      9       0
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111