2016-01-20 1279 views
3

我无法合并dataframes和无法理解为什么:类型错误:不能在非类别项追加到CategoricalIndex

简单的数据帧

df1 = pd.DataFrame({'id': np.random.randint(1,5,100), 
        'c': np.random.random(100), 
        's': np.random.random(100)}) 

分组以3个groupes

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 

日期:

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 

其次简单的数据框:

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 

试图合并这两个:

pd.merge(df_unstacked, df2, left_index=True, right_on='one') 

我希望:

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 

但我得到的类型错误:

TypeError: cannot append a non-category item to a CategoricalIndex

而且,试图在df_unsta上重置reset_index() cked,给出TypeError:

TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category

制作.copy()没有帮助:)该怎么办?

p.s.熊猫0.17.1

+0

你的'df_unstacked'列是绝对的,你如何看待'concat'?这是出现错误的地方 – EdChum

+0

Thanx EdChum,现在我明白了这个问题至少:) – arvyzu

+0

我不确定你可以绕过这个说实话,除非你用str代表覆盖列,然后合并 – EdChum

回答

1

使这项工作的一种方法是切换左右表的顺序。熊猫允许您将分类列加入非分类列,但不能以其他方式加入。

pd.merge(df2,df_unstacked, right_index=True, left_on='one') 
相关问题