您好,我有一个包含10000+行的数据帧,看起来像这样-
df = pd.DataFrame([['110', 'Demand', 2344, 30953],
['111', 'Supply', 3535, 321312],
['112', 'Supply', 35345, 2324],
['113', 'Demand', 24345, 4542],
['114', 'Supply', 342, 435623]],
columns=['Material', 'Title', '201950', '201951'])
df
Material Title 201950 201951
110 Demand 2344 30953
111 Supply 3535 321312
112 Supply 35345 2324
113 Demand 24345 4542
114 Supply 342 435623我有另一个小的数据帧,大约有4-5行,看起来像这样-
extra = pd.DataFrame([['111', 'Supply', 10],
['112', 'Supply', 20],
['114', 'Supply', 30],
['115', 'Supply', 40]],
columns=['Material', 'Title', '201950'])
extra
Material Title 201950
111 Supply 10
112 Supply 20
114 Supply 30
115 Supply 40我希望将df中列201950中的值替换为来自extra的值,只要Material和Title匹配,结果数据帧就会如下所示-
Material Title 201950 201951
110 Demand 2344 30953
111 Supply 10 321312
112 Supply 20 2324
113 Demand 24345 4542
114 Supply 30 435623我确实试过合并
updated = df.merge(extra, how='left',
on=['Material', 'Title'],
suffixes=('', '_new'))
new = '201950_new'
updated['201950'] = np.where(pd.notnull(updated[new]), updated[new], updated['201950'])
updated.drop(new, axis=1, inplace=True)这为我提供了所需的输出。但我正在寻找一种更有效的解决方案。因为df很大,而extra只有4行。
发布于 2019-12-20 13:41:00
使用DataFrame.update,但首先在两个DataFrame中创建MultiIndex by Material和Title列:
df = df.set_index(['Material','Title'])
extra = extra.set_index(['Material','Title'])
df.update(extra)
df = df.astype(int).reset_index()
print (df)
Material Title 201950 201951
0 110 Demand 2344 30953
1 111 Supply 10 321312
2 112 Supply 20 2324
3 113 Demand 24345 4542
4 114 Supply 30 435623发布于 2019-12-20 15:31:54
您可以尝试这样做:
extra.set_index(['Material','Title']).combine_first(df.set_index(['Material','Title'])).dropna().reset_index().astype(object) 输出:
Material Title 201950 201951
0 110 Demand 2344 30953
1 111 Supply 10 321312
2 112 Supply 20 2324
3 113 Demand 24345 4542
4 114 Supply 30 435623https://stackoverflow.com/questions/59420315
复制相似问题