给定一个数据框,如下所示:
city id date price
0 sh 3910060461 2008-04 19459
1 sh 1210000631 2008-05 16727
2 bj 1210000770 2008-05 12960
3 bj 1210000829 2008-05 14445
4 bj 1210001004 2008-05 16213
How could I convert date
columns horizontally and map price
into it based on city
and id
?
预期的结果是这样的:
city id 2008-04 2008-05
0 sh 3910060461 19459.0 NaN
1 sh 1210000631 NaN 16727.0
2 bj 1210000770 NaN 12960.0
3 bj 1210000829 NaN 14445.0
4 bj 1210001004 NaN 16213.0
I try df.pivot_table(columns = 'date', index=['city', 'id'], values = 'price', fill_value ='').reset_index()
, but it raises an error:
ValueError: a CategoricalDtype must be passed to perform an update, got CategoricalDtype(categories=[0], ordered=True)
You can group by all three columns of interest, then convert the dates index level to column names with
unstack
. Not a perfect solution, but it works.