Python 如何根据熊猫中的匹配列减去行?
如果我有两个数据帧,如使用创建的示例中所示:Python 如何根据熊猫中的匹配列减去行?,python,pandas,Python,Pandas,如果我有两个数据帧,如使用创建的示例中所示: df1 = pd.DataFrame({'A': randint(1,11,10), 'B': randint(10,100,10), 'C': randint(100,1000,10)}) df2 = pd.DataFrame({'A': randint(1,11,10), 'B': randint(10,100,10), 'C': randint(100,1000,10)}) df2 = df2.drop_duplicates(subset=['
df1 = pd.DataFrame({'A': randint(1,11,10), 'B': randint(10,100,10), 'C': randint(100,1000,10)})
df2 = pd.DataFrame({'A': randint(1,11,10), 'B': randint(10,100,10), 'C': randint(100,1000,10)})
df2 = df2.drop_duplicates(subset=['A'])
df1
A B C
0 2 96 826
1 1 64 601
2 1 27 343
3 5 65 600
4 10 68 658
5 6 81 895
6 5 73 440
7 4 54 865
8 1 24 597
9 10 66 928
df2
A B C
0 2 87 669
1 5 99 594
2 6 50 989
3 10 46 767
4 3 56 828
5 4 83 415
6 1 41 332
如果A列中的值匹配,如何减去B列(df['B']-df2['B'])?因此,我可以在df1中获得一个新列,如:
9
23
-14
-34
22
31
-26
-29
-17
20
试试这个:
In [61]: df1['new'] = df1.drop('C',1).merge(df2.drop('C',1), on='A',
how='left', suffixes=['','2']) \
.eval("new=B-B2", inplace=False)['new']
In [62]: df1
Out[62]:
A B C new
0 2 96 826 9
1 1 64 601 23
2 1 27 343 -14
3 5 65 600 -34
4 10 68 658 22
5 6 81 895 31
6 5 73 440 -26
7 4 54 865 -29
8 1 24 597 -17
9 10 66 928 20
要获得要减去的值,请使用
df1['A']
将df2['B']
的值映射到它,方法是使用df2['A']
索引df2['B']
:
df1['new'] = df1['B'] - df1['A'].map(df2.set_index('A')['B'])
结果输出:
A B C new
0 2 96 826 9
1 1 64 601 23
2 1 27 343 -14
3 5 65 600 -34
4 10 68 658 22
5 6 81 895 31
6 5 73 440 -26
7 4 54 865 -29
8 1 24 597 -17
9 10 66 928 20
编辑 对于较小的数据集,向
map
提供字典可能会稍微快一点
示例数据集上的计时:
%timeit df1.B - df1.A.map(df2.set_index('A').B)
%timeit df1.B - df1.A.map(dict(zip(df2.A, df2.B)))
%timeit df1.B - df1.A.map(dict(zip(df2.A.values, df2.B.values)))
1000 loops, best of 3: 718 µs per loop
1000 loops, best of 3: 492 µs per loop
1000 loops, best of 3: 459 µs per loop
对于较大的数据集,使用索引方法似乎更快
较大的数据集设置:
rows, a_max, b_max, c_max = 10**6, 5*10**4, 10**5, 10**5
df1 = pd.DataFrame({'A': randint(1, a_max, rows), 'B': randint(10, b_max, rows), 'C': randint(100, c_max, rows)})
df2 = pd.DataFrame({'A': randint(1, a_max, rows), 'B': randint(10, b_max, rows), 'C': randint(100, c_max, rows)})
df2 = df2.drop_duplicates(subset=['A'])
较大数据集上的计时:
%timeit df1.B - df1.A.map(df2.set_index('A').B)
%timeit df1.B - df1.A.map(dict(zip(df2.A, df2.B)))
%timeit df1.B - df1.A.map(dict(zip(df2.A.values, df2.B.values)))
10 loops, best of 3: 114 ms per loop
10 loops, best of 3: 359 ms per loop
10 loops, best of 3: 354 ms per loop
它起作用了!谢谢你的帮助。我选择@root-answer是因为它比较短。很短。谢谢你,巴德!