Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果dataframe中的另一列与某个值匹配,则使用pandas从dataframe中的列中减去值_Python_Pandas_Data Science - Fatal编程技术网

Python 如果dataframe中的另一列与某个值匹配,则使用pandas从dataframe中的列中减去值

Python 如果dataframe中的另一列与某个值匹配,则使用pandas从dataframe中的列中减去值,python,pandas,data-science,Python,Pandas,Data Science,假设我有两个原始矩阵和参考矩阵 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:"

假设我有两个原始矩阵和参考矩阵

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
现在我想从原始表(a)的第三列(col3)中减去参考表(c)中两个表的第二列匹配的行中的值。因此,表2的第一行应该将值10添加到第三列,因为表b中列为col2的行是'c',col3中的值为10。有道理?下面是一些代码:

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

a['col3'] = col3
print "Output Table:"
print a
想让它看起来像这样:

Output Table:
  col1 col2  col3
0    a    c   11
1    a    d   22
2    b    c   13
3    b    d   24
问题是col3在数组中使用Name:和dtype

>>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]
你能帮忙吗

这应该有效:

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

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
您可以根据要求通过以下方式将其存储在原件中:

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