0
我有兩個DataFrames DF1和DF2具有很多列如何在多索引系列中找不到NaN?
DF1 - [2756003行×44列]
DF2 - [22035行×11列]
我需要添加新列到DF2
t1 = df1.groupby(['category', 'manufacturer'])
t2=t1[c1].mean()
str1='_'.join(col)
df2[c1+'_'+str1+'_mean']=t2[df2[['category','manufacturer']].as_matrix()].values
從由結果基於組DF1 目標列的平均(在DF1 DF2和對於同一列),其返回:
IndexError: arrays used as indices must be of integer (or boolean) type
T2 - 商店多指標系列,如:
category manufacturer
1 2 0.000000
4 8.796840
10 2.312407
19 1.135094
24 4.355000
如果我使用現有的索引我會得到預期的結果
In [302]: t2[1, 2]
Out[302]: 0.0
但是,如果我叫T2 [410,332],其中332它是以df2呈現並且不以df1呈現的製造商的id,我將得到
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我想要得到NaN反而像我們從
df2['manufacturer'].map(t2)
萬一只有一列。
有 'AttributeError的: 'CategoricalIndex' 對象具有 '後 'DF2 = pd.merge(DF2,t2.to_frame(),left_on = [is_dtype_equal'' ' 類別 '' 製造商沒有屬性'], right_index =真,如何=' 左 ')' 所以我修改此部分 'DF2 = pd.merge(DF2,t2.reset_index(),left_on = [' 類別」,'製造商],right_on = ['category','manufacturer'],how ='left')' 它的工作!謝謝 – Roman
太好了,謝謝你的糾正。由於'left_on'和'right_on'指定了相同的列名,所以你可以簡單地將它設置爲'on = ['category','manufacturer']'。如果這些是2個DataFrame共享的唯一列,那麼您甚至可以完全省略它。 – unutbu