简体   繁体   English

根据条件将第1个Dataframe的第2列减去第2个Dataframe的第2列

[英]Subtracting the 2nd column of the 1st Dataframe to the 2nd column of the 2nd Dataframe based on conditions

I have 2 dataframes with 2 columns each and with different number of index.我有 2 个数据框,每个数据框有 2 列,索引数量不同。 I want to subtract the 2nd column of my 2nd dataframe to the 2nd column of my 1st dataframe, And storing the answers to another dataframe.. NOTE: Subtract only the values with the same values in column 1. eg我想将我的第二个 dataframe 的第二列减去我的第一个 dataframe 的第二列,并将答案存储到另一个 dataframe.. 注意:仅减去第 1 列中具有相同值的值。例如

df:
           col1      col2
29752   35023.0   40934.0    

subtract it to

df2:
           c1         c2
962   35023.0   40935.13  

Here is my first Dataframe:这是我的第一个 Dataframe:

           col1      col2
0      193431.0  40955.41
1      193432.0  40955.63
2      193433.0  40955.89
3      193434.0  40956.31
4      193435.0  40956.43
...       ...
29752   35023.0  40934.89
29753   35024.0  40935.00
29754   35025.0  40935.13
29755   35026.0  40934.85
29756   35027.0  40935.18

Here is my 2nd dataframe;这是我的第二个 dataframe;

          c1        c2
0     194549.0  41561.89
1     194550.0  41563.96
2     194551.0  41563.93
3     194552.0  41562.75
4     194553.0  41561.22
..       ...       ...
962  35027.0  41563.80
963  35026.0  41563.18
964  35025.0  41563.87
965  35024.0  41563.97
966  35023.0  41564.02

You can iterate for each row of the first dataframe, if the col1 of the row is equal to col1 of df2[i], then subtract the value of each column可以对第dataframe的每一行进行迭代,如果该行的col1等于df2[i]的col1,则减去每一列的值

for i, row in df1.iterrows():
if(row["col1"]  == df2["col1"][i]):
    diff = row["col2"] - df2["col2"][i]
    print(i, diff)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Dataframe: To get a column value from 2nd dataframe based on a column in the 1st dataframe is in between two columns in the 2nd dataframe - Python Dataframe: To get a column value from 2nd dataframe based on a column in the 1st dataframe is in between two columns in the 2nd dataframe Select a value from a 1 DataFrame, based on conditions from 2nd DataFrame and paste this value a new column in the 1st Dataframe - Select a value from a 1 DataFrame , based on conditions from 2nd DataFrame and paste this value a new column in the 1st Dataframe 如何根据第一个 df 中的条件在第二个 Pandas 数据框中创建和填充新列? - How to create and populate new column in 2nd Pandas dataframe based on condition in 1st df? 根据 R 中 dataframe 的另一列的相等值,在新列(在第一个数据帧中)中添加值(来自第二个数据帧) - Add value (from 2nd dataframe) in new column (in 1st dataframe) based on equality value of another column from both dataframe in R 读取panda dataframe的第1列、第2列、第n列到最后一列 - Read 1st column, 2nd column, and nth column to last column of panda dataframe 用数据框中的“第 2 天”和“第 1 天”替换一列中的最后 2 个日期,以使代码动态化 - replace the last 2 dates in one column by "2nd day" and "1st day" in a dataframe to make the code dynamic 将 pandas 数据框列中的每个值与第二个数据框列的所有值相乘并将每个第一个数据框值替换为结果数组 - Multiply each value in a pandas dataframe column with all values of 2nd dataframe column & replace each 1st dataframe value with resulting array 根据两列的比较从第二个DataFrame添加列 - Add column from 2nd DataFrame based on comparison of two columns 根据第一列中的最大日期按月创建第二列 - Create a 2nd column based on the maximum date By Month in 1st column 比较 2 个数据帧并遍历第一个 dataframe 每一行,以便验证第二个 df 中的相应列值 - compare 2 dataframes and traverse 1st dataframe each n evry row so that validate corresponding column values in 2nd df
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM