如果值在另一列中,则来自另一个DataFrame的pandas列? [英] pandas columns from another DataFrame if value is in another column?
问题描述
我有pd.DataFrame列出了线规及其相应的电流:
I have pd.DataFrame listing wire gauges and their corresponding currents:
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列出了系统中的断路器:
Another DataFrame lists breakers in a system:
system =
breakers
0 30
1 20
2 30
3 15
4 30
我需要通过查找e_ref当前序列中的断路器值,从e_ref数据框架的"wire_gauge"列中向系统DataFrame添加一个线规"列.
I need to add a "wire gauge" column to the system DataFrame from the "wire_gauge" columns of the e_ref DataFrame by looking up the breaker value in the current series of the e_ref.
所以输出将是:
breakers wire_gauge
0 30 10
1 20 12
2 30 10
3 15 14
4 30 10
我一直在混淆其他帖子中的几个答案,并且目前没有有效的熊猫解决方案.我可以使用python循环使它正常工作,但是我觉得这里有一只熊猫内胆...
I keep confusing several answers from other post and currently do not have a working pandas solution. I can get this working using python loops but I feel like there is a pandas one liner here...
以下是我正在研究的解决方案类型:
Below are the types of solutions I'm working on:
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创建的e_ref
或 map
. join.html"rel =" nofollow noreferrer> join
,但e_ref
中current
列中的必要值必须唯一:
Use map
by Series
created form e_ref
or join
, but is necessary values in current
column in e_ref
has to be unique:
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
这篇关于如果值在另一列中,则来自另一个DataFrame的pandas列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!