0

I have a pandas DataFrame named adjusted_df_reindexed as below. It was converted from an xarray DataArray object.

     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

It has two columns region_id and Daily Count and the index is the dates spanning 365 days. The region_id are 0, 1, 2, 3, 4, 5. Each region_id has 365 rows.

I would like the unique values of region_id (0, 1, 2, 3, 4, 5) to be the columns and the Daily Count remains values of the table.

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
alextc
  • 3,206
  • 10
  • 63
  • 107

1 Answers1

1

A pivot table is definitely one way to do it:

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

Josh Gel
  • 31
  • 2