简体   繁体   中英

substract values from column in dataframe if another column in dataframe matches some value using pandas

say I have two matrix original and reference

import pandas as pa
print "Original Data Frame"
# Create a dataframe
oldcols = {'col1':['a','a','b','b'], 'col2':['c','d','c','d'], 'col3':[1,2,3,4]}
a = pa.DataFrame(oldcols)
print "Original Table:"
print a

print "Reference Table:"
b = pa.DataFrame({'col1':['x','x'], 'col2':['c','d'], 'col3':[10,20]})
print b

Now I want to subtract from the third column (col3) of the original table (a), the value in the reference table (c) in the row where the second columns of the two tables match. So the first row of table two should have the value 10 added to the third column, because the row of table b where the column is col2 is 'c' has a value of 10 in col3. Make sense? Here's some code that does that:

col3 = []
for ix, row in a.iterrows():
    col3 += [row[2] + b[b['col2'] == row[1]]['col3']]

a['col3'] = col3
print "Output Table:"
print a

and want to make it look like this:

Output Table:
  col1 col2  col3
0    a    c   11
1    a    d   22
2    b    c   13
3    b    d   24

the problem is col3 takes Name: and dtype in a array

>>print col3
[0    11
Name: col3, dtype: int64, 1    22
Name: col3, dtype: int64, 0    13
Name: col3, dtype: int64, 1    24
Name: col3, dtype: int64]

Can you please help?

This should work:

a['col3'] + a['col2'].map(b.set_index('col2')['col3'])
Out[94]: 
0    11
1    22
2    13
3    24
dtype: int64

Or this:

a.merge(b, on='col2', how='left')[['col3_x', 'col3_y']].sum(axis=1)
Out[110]: 
0    11
1    22
2    13
3    24
dtype: int64

You can store this in the original, as requested, through:

a['col3'] = a.merge(b, on='col2', how='left')[['col3_x', 'col3_y']].sum(axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM