[英]pandas columns from another DataFrame if value is in another column?
I have pd.DataFrame listing wire gauges and their corresponding currents: 我有pd.DataFrame列出了线规及其相应的电流:
e_ref = 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
Another DataFrame lists breakers in a system: 另一个DataFrame列出了系统中的断路器:
system = 系统=
breakers
0 30
1 20
2 30
3 15
4 30
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. 我需要通过在e_ref数据帧的当前系列中查找断路器值,从e_ref数据帧的“ wire_gauge”列中向系统DataFrame添加“线规”列。
so the output would be: 因此输出为:
breakers wire_gauge
0 30 10
1 20 12
2 30 10
3 15 14
4 30 10
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...
我可以使用python循环使它工作,但我感觉这里有一只熊猫班轮...
Below are the types of solutions I'm working on: 以下是我正在研究的解决方案类型:
df.ix[df.breakers.isin(e_ref['current']), 'wire_gauge'] = e_ref['wire_gauge']
and 和
df['wire_gauge']=e_ref.loc[e_ref['current'] == df['breakers'] ]
Thanks for your time and direction! 感谢您的时间和方向!
Use map
by Series
created form e_ref
or join
, but is necessary values in current
column in e_ref
has to be unique: 使用按
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
Alternative: 替代方案:
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.