嗨,您有一个包含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
我想使用
201950
和df
匹配的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行。 最佳答案
使用DataFrame.update
,但首先在两个MultiIndex
中均由Material
和Title
列创建DataFrame
:
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