pandas 类型错误:不能将非类别项目附加到 CategoricalIndex
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34901708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: cannot append a non-category item to a CategoricalIndex
提问by arvyzu
I cannot merge dataframes and cannot understand why:
我无法合并数据帧,也不明白为什么:
Simple dataframe
简单的数据框
df1 = pd.DataFrame({'id': np.random.randint(1,5,100),
'c': np.random.random(100),
's': np.random.random(100)})
grouped to 3 groupes
分成3组
grouped = pd.qcut(df1.c, 3)
df_grouped = df1.groupby([grouped, 'id'])
df_cross = df_grouped['s'].sum()
df_unstacked = df_cross.unstack(level=0)
df_unstacked
Out:
出去:
c [0.018, 0.372] (0.372, 0.771] (0.771, 0.995]
id
1 3.081537 6.329819 3.386422
2 4.270542 2.553301 3.778536
3 3.125476 2.525016 3.013912
4 5.762223 3.763183 7.953551
Second simple dataframe:
第二个简单的数据框:
df2 = pd.DataFrame({'one': range(5),
'two': np.random.randint(1,5,5),
'three': ['a', 'a', 'a', 'b', 'b']})
one three two
0 0 a 4
1 1 a 2
2 2 a 1
3 3 b 2
4 4 b 2
Trying to merge both:
试图合并两者:
pd.merge(df_unstacked, df2, left_index=True, right_on='one')
I would expect:
我希望:
c [0.018, 0.372] (0.372, 0.771] (0.771, 0.995] one three two
id
1 3.081537 6.329819 3.386422 1 a 2
2 4.270542 2.553301 3.778536 2 a 1
3 3.125476 2.525016 3.013912 3 b 2
4 5.762223 3.763183 7.953551 4 b 2
But I get TypeError:
但我得到了类型错误:
TypeError: cannot append a non-category item to a CategoricalIndex
类型错误:不能将非类别项目附加到 CategoricalIndex
Also, trying to reset_index() on df_unstacked, gives TypeError:
此外,尝试在 df_unstacked 上 reset_index() 会给出 TypeError:
TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category
类型错误:无法将项目插入还不是现有类别的 CategoricalIndex
Making a .copy() does not help :) what to do?
制作 .copy() 没有帮助:) 怎么办?
p.s. pandas 0.17.1
psPandas0.17.1
回答by Nick C BK
One way to make this work is to switch the order of the left and right tables. Pandas allows you to join the Categorical columns to non-Categorical ones, but not the other way around.
使这项工作的一种方法是切换左表和右表的顺序。Pandas 允许您将 Categorical 列加入到非 Categorical 列中,但反过来不行。
pd.merge(df2,df_unstacked, right_index=True, left_on='one')