在同一行和同一列中减去不同数据框文件中的值

时间:2018-07-09 21:07:29

标签: pandas dataframe

我有两个具有很多列的数据集。我想对所有数据点减去相同的行号和列号中的值。这只是这两种数据的一小部分。

数据1:

4   6   8
4403    4403    4403
4640    4640    4640
0   0   0
12  0   12
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   12
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
12  0   0
0   0   0
0   0   0
12  0   0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
12  0   0
0   0   0
0   0   0
127 60  60
357 275 317
1882    2144    1838
6726    6609    7915
9398    11180   12737
12784   18389   21361
15863   20111   24469
6739    10202   11897
1684    1921    2735
249 376 476
47  103 70
0   26  82
17  0   18
0   0   0
0   0   0
0   0   0
0   0   0
0   0   18
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   12
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   12
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
12  0   0
0   0   0
0   0   0
0   0   0
0   0   0
12  0   0
0   0   12
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
12  0   0
0   0   0
12  0   0
0   0   0
0   0   12
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   18
0   0   0
0   0   0
0   0   0
12  0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   12
12  0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
12  0   0
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   0   0
12  0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
12  0   12
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   12
0   0   0
12  0   0
12  0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   0
0   0   0
13  0   0
0   0   12
0   0   0
0   0   13

数据2:

4   6   8
4400    4400    4400
4750    4750    4750
0   0   0
12  0   0
0   0   0
0   12  0
0   0   0
0   25  12
0   0   12
12  13  0
0   0   0
0   12  0
0   0   0
12  0   0
0   0   0
0   12  0
0   0   0
12  0   0
0   0   0
0   0   12
12  0   12
0   0   0
24  0   18
0   0   0
0   0   0
12  0   0
0   0   0
0   0   12
0   12  0
39  0   0
0   12  0
0   0   0
0   12  12
0   0   0
0   0   0
0   0   0
0   0   12
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
0   0   0
0   0   0
0   0   0
43  46  13
6711    11323   9375
91043   116679  123466
241572  307822  310620
250588  309749  314146
105123  139651  141462
16143   21264   23856
2521    3648    3243
1042    1022    1598
576 910 525
482 552 509
229 416 425
210 227 264
120 149 99
69  55  58
47  0   17
26  65  29
0   20  35
0   0   32
0   0   14
0   12  12
12  38  12
0   0   0
18  0   12
0   0   0
0   13  0
0   0   0
0   18  0
16  0   12
12  0   0
0   0   12
12  0   12
0   0   0
0   23  0
0   0   0
0   0   0
20  0   0
0   0   0
0   0   0
0   0   0
0   0   12
0   12  12
14  12  0
0   0   0
0   0   0
0   0   12
0   0   0
0   12  0
0   0   0
0   0   0
12  0   12
0   0   0
0   0   0
0   18  0
0   0   0
12  0   39
12  0   12
13  0   12
0   0   0
0   0   0
0   0   0
0   0   0
24  0   14
0   15  0
0   16  0
0   12  0
12  0   12
0   12  0
0   0   0
0   0   0
14  13  0
0   23  24
0   0   0
0   0   12
0   16  0
0   12  0
0   0   12
0   0   0
0   0   0
0   13  0
0   0   0
0   0   0
16  0   0
0   12  21
0   0   0
0   0   0
0   12  13
0   0   0
0   0   0
0   19  12
0   0   0
0   0   12
0   12  0
0   0   0
0   12  12
0   12  0
0   0   0
0   0   0
12  24  0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   12  0
12  0   0
0   0   0
0   0   29
0   0   0
0   0   12
0   0   0
0   12  0
12  12  0
0   0   0
12  0   0
0   0   0
0   0   0
0   0   12
0   12  0
0   0   0
12  0   0
0   0   0
0   0   0
12  0   0
0   0   0
12  0   0
0   0   12
13  16  12
24  17  0
0   0   0
12  0   41
0   0   0
0   0   0
12  0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
14  12  0
25  0   0
0   12  0
0   0   0
0   15  0
0   0   0
12  0   0
0   0   0
0   0   0
0   0   25
27  0   0
0   12  0
0   22  0
0   0   0
0   0   0
0   0   0

对于所有数据,列名称为“ 4”,“ 6”,“ 8”。

因此,在数据1和2之间,列'4'中第1行的第一个减法将是= 4403-4400。

单独进行操作比较容易,但是我正在寻找一种有效地对所有列进行此操作的方法。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

编辑:这是两个答案。如上所述的短代码:

result = Data1 - Data2

如果要用更复杂的计算代替减法,可能需要更长的答案。它首先创建结果DataFrame。假定Data1和Data2的形状相同。正如Python中常见的一样,还有其他方法可以做到这一点。这次我测试了。 :-)

result = pd.DataFrame(columns = ['4','6','8'], index = Data1.index)
for index, row in Data1.iterrows():
    for col in Data1.columns:
        result.iloc[index][col] = Data1.iloc[index][col] - Data2.iloc[index][col]
print (result)