Pandas columns from another DataFrame, if the value is in another column?

I have a 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

Another DataFrame contains switch lists in the system:

system =

    breakers
0         30
1         20
2         30
3         15
4         30

I need to add the column "explorer" to the DataFrame system from the columns "wire_gauge" e_ref DataFrame, looking at the interrupt value in the current series e_ref.

so the output will be:

    breakers  wire_gauge
0         30  10
1         20  12
2         30  10
3         15  14
4         30  10

I continue to mix multiple responses from another post and currently do not have a working pandas solution. I can make this work with python loops, but I feel there is one here

The following 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'] ]

Thank you for your time and direction!

+1
1

map Series e_ref join, current e_ref :

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
+1

Source: https://habr.com/ru/post/1680812/


All Articles