简体   繁体   English

如何使用python将一列的每个单元格与csv文件中另一列的每个单元格进行比较?

[英]How to compare each cells of one columns to each cells of another column in a csv file with python?

I have a program that uses python pandas library to sum two columns individually and compare with 3rd column and give result.我有一个程序使用 python pandas 库分别对两列求和并与第三列进行比较并给出结果。 Its below:它在下面:

import pandas as pd

df = pd.read_csv(r'xl1.csv', skipinitialspace=True, sep=',')
sum1 = df['Gross_Salary'].sum()
sum2 = df['Deduction'].sum()
diff = sum1 - sum2

if diff == df['Net_Salary'].sum():
    print("Pass")
else:
    print("Fail")

Its working as required.它按要求工作。 However, my requirement is to compare each cell of both columns, subtract them, and then compare with the 3rd column.但是,我的要求是比较两列的每个单元格,减去它们,然后与第三列进行比较。 If the value matches then "pass", otherwise "fail"如果值匹配则“通过”,否则“失败”

Below is the CSV data:以下是 CSV 数据:

Gross_Salary Deduction Net_Salary
100             20         80
2000            200       1500
300             0          300

In the 2nd row,there is a data mismatch intentionally.在第 2 行,故意存在数据不匹配。

I understand that I need to use for loop to go over each row.我知道我需要使用 for 循环来遍历每一行。 I did try to use the loop as below我确实尝试使用如下循环

for i in pd.read_csv(r'xl1.csv', skipinitialspace=True, sep=',')

However, not able to apply the logic beyond that.但是,无法应用除此之外的逻辑。

Please help,请帮忙,

Thank you谢谢

You can create a new column storing the test result using a vectorized implementation.您可以使用矢量化实现创建一个存储测试结果的新列。 Namely:即:

df['Result'] = ((df['Gross_Salary'] - df['Deduction']) == df['Net_Salary']).astype(int)
df['Result'] = df['Result'].map({1: 'Pass', 0: 'Fail'})

or similarly, if you also have numpy dependency:或者类似地,如果你也有 numpy 依赖:

df['Result'] = np.where(df['Gross_Salary'] - df['Deduction'] == df['Net_Salary'],
                        'Pass', 'Fail')

Pandas implementation熊猫实现

  • df['Gross_Salary'] - df['Deduction'] computes the elementwise difference of the two columns. df['Gross_Salary'] - df['Deduction']计算两列的元素差异。 Remark that pandas automatically aligns elements with the same index.请注意,pandas 会自动将具有相同索引的元素对齐。
  • Once we have the difference we compare it elementwise with df['Net_Salary'] using the == operator.一旦我们有了差异,我们就使用==运算符将其与df['Net_Salary']进行比较。 This will yield Series (column) with boolean values.这将产生具有布尔值的系列(列)。
  • I am converting to int type so that True -> 1 and False -> 0我正在转换为int类型,以便True -> 1False -> 0
  • Finally I am using Series.map to encode the desired format, mapping 1 to Pass and 0 to Fail .最后,我使用Series.map对所需的格式进行编码,将 1 映射到Pass并将 0 映射到Fail

Numpy implementation Numpy 实现

  • np.where returns the second (resp. the third) value depending if the condition (the first parameter) is True (resp. False). np.where返回第二个(相应的第三个)值,具体取决于条件(第一个参数)是否为 True(相应的 False)。

Applying one of these to your example:将其中之一应用于您的示例:

df
    Gross_Salary  Deduction  Net_Salary Result
0           100         20          80   Pass
1          2000        200        1500   Fail
2           300          0         300   Pass

暂无
暂无

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

相关问题 根据另一个数据框中的列有条件地格式化每列中的单元格 - Conditionally format cells in each column based on columns in another dataframe Python CSV将单元从一个csv文件复制到另一个 - Python CSV copying cells from one csv file to another 在Python中的csv文件中比较一列到另一列 - compare one column to another in csv file in Python 如何为单独的单元格中的每个值设置python生成的表* .CSV - How to set python generated table for each value in separate cells *.CSV 如何使用 python 将 csv 文件中的字符串分隔为每个分号上的多个单元格 - How to separate a string in a csv file into multiple cells on each semicolon using python 如何使用python计算每列excel中填充单元格的数量 - how to count number of filled cells in each column of excel using python 如何使用python更新基于同一csv文件中其他列单元格的列单元格中的值? - How to update value in column cells based on other column cells in the same csv file bys using python? python如何迭代CSV文件中同一列中的单元格? - python how to iterate cells in the same column in a CSV file? Python Pandas:如何比较单元格和两列的值以及如何使用 If...Else 语句创建具有新值的另一列 - Python Pandas: How to compare values of cells and two columns and maybe using If...Else statement to create another column with new values 使用 Python 3.8 将一个 CSV 文件中的一列(向量)与另一个 CSV 文件中的两列(向量和数组)进行比较 - Compare one column (vector) from one CSV file with two columns (vector and array) from another CSV file using Python 3.8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM