Python 熊猫基于来自不同数据帧的变量添加新列
我有以下数据帧,我们称之为df1Python 熊猫基于来自不同数据帧的变量添加新列,python,pandas,Python,Pandas,我有以下数据帧,我们称之为df1 id level a b c d 1 One 1 3 4 4 1 two 1 3 4 2 One 1 3 4 4 2 two 1 3 4 然后,还有第二个数据帧df2 id Type Value 1 a 11 1 b
id level a b c d
1 One 1 3 4 4
1 two 1 3 4
2 One 1 3 4 4
2 two 1 3 4
然后,还有第二个数据帧df2
id Type Value
1 a 11
1 b 22
1 c 33
1 d 44
2 a 91
2 b 92
2 c 93
2 d 94
我想要的是比较df1和df2中的值,以添加新列,如下所示。
添加名为type
\ucode>s的新列,其中包含来自df2的值
id level a b c d a_s b_s c_s d_s
1 One 1 3 4 4 11 22 33 44
1 two 1 3 4 11 22 33 44
2 One 1 3 4 4 91 92 93 94
2 two 1 3 4 91 92 93 94
堆叠、取消堆叠df2并与df合并
pd.merge(df,df2.set_index(['id','Type']).stack().unstack('Type').reset_index().drop(columns=['level_1']), how='left',on='id',suffixes=('', '_s'))
id level a b c d a_s b_s c_s d_s
0 1 One 1 3 4 4.0 11 22 33 44
1 1 two 1 3 4 NaN 11 22 33 44
2 2 One 1 3 4 4.0 91 92 93 94
3 2 two 1 3 4 NaN 91 92 93 94
或透视df2并合并
pd.merge(df,pd.pivot(df2, index='id', columns='Type').droplevel(0, axis=1).reset_index(), how='left',on='id',suffixes=('', '_s'))
谢谢但我从这两个建议中都得到了这个错误:
ValueError:Index包含重复的条目,无法重塑
Ok,pd.merge(df,df2.pivot\u表(Index='id',columns='Type').droplevel(0,axis=1).reset\u Index(),how='left',on='id',后缀=('','u s'))
您的id
和Type
中肯定都有重复项。使用df.pivot_表,因为它能够对分层多索引进行排序<代码>pd.pivot使用指定索引中的唯一值。您的示例的不足之处在于它在id
和Type
中都没有重复项。如果有帮助的话