Python 在CSV中转换特定数据

Python 在CSV中转换特定数据,python,csv,pandas,Python,Csv,Pandas,我有一个csv文件,如下所示: 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 19

我有一个csv文件,如下所示:

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
Year  Month   1     2     3      4     5 
 1912   4     56     56    74     82    79
 1913   4     73     60    67     81    77
我希望它看起来像这样:

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
Year  Month   1     2     3      4     5 
 1912   4     56     56    74     82    79
 1913   4     73     60    67     81    77

因此,每个
Day
现在都是列标题,
Tmax
显示为列而不是行。我已经尝试过解压和换位,但似乎还没有完全理解。任何帮助都将不胜感激。

您可以使用
pd.pivot_table()
'Tmax'
值上的
索引
作为
'Day'

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

FWIW,也可以这样做:
df.set_index(['Year','Month','Day']).unstack()