![](/img/trans.png)
[英]Most Efficient way to get index of item within a group in pandas dataframe
[英]most efficient way to update pandas dataframe when index not match
我有兩個pandas DataFrames,我想用另一個更新一個...但我不能確定索引是否匹配。 (所以使用DataFrame.update是個問題!)
〔實施例:
import pandas as pd
df1 = pd.DataFrame([('path1', 0, 0, 0),
('path2', 0, 0, 0),
('path3', 0, 0, 0),
('path4', 0, 0, 0),],
columns=['path', 'class', 'manual', 'conf'],
index = [1,2,3,4])
df2 = pd.DataFrame([('path1', 1, 0, 0),
('path2', 0, 1, 0),
('path3', 0, 0, 1),
('path5', 1, 1, 0),
('path6', 1, 1, 0),],
columns=['path', 'class', 'manual', 'conf'],
index = [10,11,12,13,14])
期望的結果:
update_annotations(df1, df2)
path class manual conf
1 path1 1 0 0
2 path2 0 1 0
3 path3 0 0 1
4 path4 0 0 0
df1.update(df2)可能存在風險,因為這些數據幀的索引可能不匹配。 這樣做最安全,最有效的方法是什么?
df1[['path']].merge(df2, 'left')
path class manual conf
0 path1 1.0 0.0 0.0
1 path2 0.0 1.0 0.0
2 path3 0.0 0.0 1.0
3 path4 NaN NaN NaN
df1[['path']].merge(df2, 'left').fillna(0).astype(df1.dtypes)
path class manual conf
0 path1 1 0 0
1 path2 0 1 0
2 path3 0 0 1
3 path4 0 0 0
用df1
填充NaN
df1[['path']].merge(df2, 'left').fillna({**df1}).astype(df1.dtypes)
path class manual conf
0 path1 1 0 0
1 path2 0 1 0
2 path3 0 0 1
3 path4 0 0 0
df1.set_index('path').assign(**df2.set_index('path')).reset_index()
path class manual conf
0 path1 1.0 0.0 0.0
1 path2 0.0 1.0 0.0
2 path3 0.0 0.0 1.0
3 path4 NaN NaN NaN
由於訂單保證相同,我們可以使用set_index
df1[['path']].merge(df2, 'left').fillna({**df1}).astype(df1.dtypes).set_index(df1.index)
path class manual conf
1 path1 1 0 0
2 path2 0 1 0
3 path3 0 0 1
4 path4 0 0 0
根據piRSquared的答案,我正在尋找答案:
df1 = (df1[['path']]
.merge(df2, 'left')
.set_index(df1.index)
.fillna(df1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.