[英]Replacing value in a column in pandas dataframe from a column value in another dataframe
[英]pandas columns from another DataFrame if value is in another column?
我有pd.DataFrame列出了線規及其相應的電流:
e_ref =
wire_gauge current
0 14 15
1 12 20
2 10 30
3 8 50
4 6 60
5 4 85
6 3 100
7 2 115
另一個DataFrame列出了系統中的斷路器:
系統=
breakers
0 30
1 20
2 30
3 15
4 30
我需要通過在e_ref數據幀的當前系列中查找斷路器值,從e_ref數據幀的“ wire_gauge”列中向系統DataFrame添加“線規”列。
因此輸出為:
breakers wire_gauge
0 30 10
1 20 12
2 30 10
3 15 14
4 30 10
我一直在混淆其他帖子的幾個答案,並且目前沒有有效的熊貓解決方案。 我可以使用python循環使它工作,但我感覺這里有一只熊貓班輪...
以下是我正在研究的解決方案類型:
df.ix[df.breakers.isin(e_ref['current']), 'wire_gauge'] = e_ref['wire_gauge']
和
df['wire_gauge']=e_ref.loc[e_ref['current'] == df['breakers'] ]
感謝您的時間和方向!
使用按Series
創建的map
e_ref
或join
形式,但是e_ref
current
列中的必要值必須唯一:
print (e_ref['current'].is_unique)
True
s = e_ref.set_index('current')['wire_gauge']
system['wire_gauge'] = system['breakers'].map(s)
print (system)
breakers wire_gauge
0 30 10
1 20 12
2 30 10
3 15 14
4 30 10
替代方案:
df = system.join(e_ref.set_index('current'), on='breakers')
print (df)
breakers wire_gauge
0 30 10
1 20 12
2 30 10
3 15 14
4 30 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.