【发布时间】:2020-10-26 04:11:06
【问题描述】:
给定一个数据框如下:
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
如何水平转换date 列并根据city 和id 将price 映射到其中?
预期的结果是这样的:
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
我尝试df.pivot_table(columns = 'date', index=['city', 'id'], values = 'price', fill_value ='').reset_index(),但它引发了错误:
ValueError: a CategoricalDtype must be passed to perform an update, got CategoricalDtype(categories=[0], ordered=True)
【问题讨论】:
标签: python-3.x pandas dataframe pivot-table