我有兩個資料框。我想更改一些值。
我知道如何逐一更改它,使用 isin 和 where 陳述句,但我不知道如何更改大量更改。
df1
Name Type
David Staff
Jones Pilot
Jack Pilot
Susan Steward
John Staff
Leroy Staff
Steve Staff
df2
Name Type
David Captain
Leroy Pilot
Steve Pilot
如何使用 df2 更改 df1 上的“型別”列?
df_desired
Name Type
David Captain
Jones Pilot
Jack Pilot
Susan Steward
John Staff
Leroy Pilot
Steve Pilot
uj5u.com熱心網友回復:
您可以嘗試地圖Type列然后df2df1update
df1['Type'].update(df1['Name'].map(df2.set_index('Name')['Type']))
print(df1)
Name Type
0 David Captain
1 Jones Pilot
2 Jack Pilot
3 Susan Steward
4 John Staff
5 Leroy Pilot
6 Steve Pilot
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/482612.html
上一篇:請幫我找出我錯過了什么案例
