我有一个奇怪的熊猫问题.
我有一个主数据帧:
a b c
0 22 44 55
1 22 45 22
2 44 23 56
3 45 22 33
Run Code Online (Sandbox Code Playgroud)
然后,我有一个不同维度的数据框,其中包含一些重叠索引和列名称
index col_name new_value
0 a 111
3 b 234
Run Code Online (Sandbox Code Playgroud)
我试着说,如果在主数据帧中找到索引和col_name匹配,则替换该值.
所以输出就是
a b c
0 111 44 55
1 22 45 22
2 44 23 56
3 45 234 33
Run Code Online (Sandbox Code Playgroud)
我找到了"Combine_first",但这不起作用,除非我转动第二个数据帧(在这种情况下我无法做到)
这是个update问题
df.update(updated.pivot(*updated.columns))
df
Out[479]:
a b c
0 111.0 44.0 55
1 22.0 45.0 22
2 44.0 23.0 56
3 45.0 234.0 33
Run Code Online (Sandbox Code Playgroud)
要么
df.values[updated['index'].values,df.columns.get_indexer(updated.col_name)]=updated.new_value.values
df
Out[495]:
a b c
0 111 44 55
1 22 45 22
2 44 23 56
3 45 234 33
Run Code Online (Sandbox Code Playgroud)