Transpose specific data to CSV

I have a csv file that looks like this:

Month     Day   Year  Tmax
   4       1    1912    56
   4       2    1912    56
   4       3    1912    74
   4       4    1912    82
   4       5    1912    79
   4       1    1913    73
   4       2    1913    60
   4       3    1913    67
   4       4    1913    81
   4       5    1913    77

      

and I want it to look like this:

Year  Month   1     2     3      4     5 
 1912   4     56     56    74     82    79
 1913   4     73     60    67     81    77

      

So each is Day

now a column heading and is Tmax

displayed as columns instead of rows. I played around with unboxing and transposition but couldn't seem to get it. Any help would be appreciated.

+3


source to share


1 answer


You can use pd.pivot_table()

with index

both ['Year', 'Month']

and columns

both 'Day'

of 'Tmax'

the values.



In [10]: pd.pivot_table(df, values='Tmax',
                        index=['Year', 'Month'],
                        columns='Day')
Out[10]:
Day          1   2   3   4   5
Year Month
1912 4      56  56  74  82  79
1913 4      73  60  67  81  77

      

+3


source