简体   繁体   English

如果另一个列中的值是另一个DataFrame中的pandas列?

[英]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_refjoin形式,但是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.

相关问题 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe Pandas:如果数据框中的值包含来自另一个数据帧的字符串,则追加列 - Pandas : if value in a dataframe contains string from another dataframe, append columns 根据熊猫数据框中另一列的最后一个值填充列 - Fill columns based on the last value of another column in a pandas dataframe 使用来自另一个具有条件的数据帧的值更新熊猫数据帧列 - update pandas dataframe column with value from another dataframe with condition Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值 - Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame 如何从另一个 Pandas dataframe 逐列缩放 - how to scale columns by column from another Pandas dataframe 熊猫列值从另一个数据框值更新 - pandas column value update from another dataframe value pandas DataFrame:用另一个值替换多个列中的值 - pandas DataFrame: replace values in multiple columns with the value from another 如何计算 Pandas dataframe 中同时包含一组列中的值和另一列中的另一个值的行数? - How to count the number of rows containing both a value in a set of columns and another value in another column in a Pandas dataframe? 如果等于另一列中的值,则熊猫从多列返回值 - Pandas return value from multiple columns if equal to value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM