【发布时间】:2018-08-18 03:04:44
【问题描述】:
我知道了。数据框:
ps_yd_1 $0^{th} - 25^{th}$ $25^{th} - 50^{th}$ \
ps_variable_1
$0^{th} - 25^{th}$ 47.566800 23.441332
$25^{th} - 50^{th}$ 32.764905 40.947438
$50^{th} - 75^{th}$ 10.830286 21.435877
$75^{th} - 100^{th}$ 14.388537 33.796734
ps_yd_1 $50^{th} - 75^{th}$ $75^{th} - 100^{th}$
ps_variable_1
$0^{th} - 25^{th}$ 21.237253 7.754615
$25^{th} - 50^{th}$ 8.634613 17.653044
$50^{th} - 75^{th}$ 14.684188 53.049650
$75^{th} - 100^{th}$ 13.072976 38.741753
我想添加 2 列来创建一个新的:
df_hmp['a'] = df_hmp['$0^{th} - 25^{th}$'] + df_hmp['$25^{th} - 50^{th}$']
但我收到此错误:
*** TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category
这是索引的样子:
CategoricalIndex(['$0^{th} - 25^{th}$', '$25^{th} - 50^{th}$',
'$50^{th} - 75^{th}$', '$75^{th} - 100^{th}$'],
categories=['$0^{th} - 25^{th}$', '$25^{th} - 50^{th}$', '$50^{th} - 75^{th}$', '$75^{th} - 100^{th}$'], ordered=True, name='ps_variable_1', dtype='category')
如何解决?
【问题讨论】:
-
请提供易于复制到 IDE 中的示例数据。特别是在这种情况下,如果不进行大量重新配置,就很难重现导致此问题的条件。
标签: python pandas dataframe categorical-data