大熊猫-将列的值更改为列

时间:2019-07-05 00:49:32

标签: python pandas

我有一个名为adjusted_df_reindexed的熊猫DataFrame,如下所示。它是从xarray DataArray对象转换而成的。

     region_id  Daily Count
time        
2009-01-01  0   431.0
2009-01-02  0   1107.0
2009-01-03  0   1107.0
2009-01-04  0   1106.0
2009-01-05  0   954.0
2009-01-06  0   1089.0
2009-01-07  0   718.0
2009-01-08  0   1027.0
2009-01-09  0   925.0
2009-01-10  0   1076.0
2009-01-11  0   1096.0
2009-01-12  0   1093.0
2009-01-13  0   578.0
2009-01-14  0   764.0
2009-01-15  0   135.0
2009-01-16  0   937.0
2009-01-17  0   1066.0
2009-01-18  0   1034.0
2009-01-19  0   1058.0
2009-01-20  0   1033.0
2009-01-21  0   1010.0
2009-01-22  0   731.0
2009-01-23  0   496.0
2009-01-24  0   971.0
2009-01-25  0   992.0
2009-01-26  0   928.0
2009-01-27  0   739.0
2009-01-28  0   345.0
2009-01-29  0   857.0
2009-01-30  0   801.0
...     ...     ...
2009-12-02  5   317.0
2009-12-03  5   260.0
2009-12-04  5   310.0
2009-12-05  5   316.0
2009-12-06  5   330.0
2009-12-07  5   327.0
2009-12-08  5   314.0
2009-12-09  5   290.0
2009-12-10  5   162.0
2009-12-11  5   237.0
2009-12-12  5   246.0
2009-12-13  5   314.0
2009-12-14  5   330.0
2009-12-15  5   330.0
2009-12-16  5   279.0
2009-12-17  5   264.0
2009-12-18  5   289.0
2009-12-19  5   346.0
2009-12-20  5   346.0
2009-12-21  5   346.0
2009-12-22  5   340.0
2009-12-23  5   251.0
2009-12-24  5   266.0
2009-12-25  5   286.0
2009-12-26  5   321.0
2009-12-27  5   326.0
2009-12-28  5   324.0
2009-12-29  5   312.0
2009-12-30  5   244.0
2009-12-31  5   232.0

2190 rows × 2 columns

它有两列region_idDaily Count,索引是跨越365天的日期。 region_id为0、1、2、3、4、5。每个region_id具有365行。

我希望将region_id的唯一值(0、1、2、3、4、5)作为列,而“每日计数”仍是表格的值。

time          0       1       2       3       4       5
2009-01-01   431     202     ...     ...     ...     ...
2009-01-02   1107    501     ...     ...     ...     ...
2009-01-03   1107    501     ...     ...     ...     ...
2009-01-04   1106    478     ...     ...     ...     ...
2009-01-05   954     421     ...     ...     ...     ...
2009-01-06   1089    489     ...     ...     ...     ...
2009-01-07   718     400     ...     ...     ...     ...
     ...     ...     ...     ...     ...     ...     ...
     ...     ...     ...     ...     ...     ...     ...
2009-12-30   728     398     ...     ...     ...     244
2009-12-31   645     516     ...     ...     ...     232

1 个答案:

答案 0 :(得分:1)

数据透视表绝对是一种实现方式:

df.reset_index(drop=False).pivot(index='time', columns='region_id', values='Daily Count')