【问题标题】:How Iterate each element row and compare with another elements in row [duplicate]如何迭代每个元素行并与行中的另一个元素进行比较
【发布时间】:2022-11-15 14:43:38
【问题描述】:

我必须从本地机器中的 CSV 获取值并迭代每个元素并与另一行的每个元素进行比较。

value comparison between source and target

我的 CSV 存储在我的本地 C 驱动器中并读取值,现在我需要帮助来迭代源和目标中的每个元素。

import csv
with open('C:\\Users\\user\\Desktop\\test_readwrite.csv') as cs:
csv_reader = csv.reader(cs)
#displaying it line by line
for line in csv_reader:
    print(line)
#closing the connection
cs.close()

【问题讨论】:

标签: python opencsv sift


【解决方案1】:

我很确定这可以被标记为重复。 尽管如此,使用 pandas 应该更容易比较。

import pandas as pd

df = pd.read_csv('data.csv')

# Compare column 1 and column 2
def compare(x, y):
    # Your condition, return true or false
    # I am using equality
    return x == y

df['result'] = df.apply(lambda x: compare(x['col1'], x['col2']), axis=1)

这应该可以满足您的要求

【讨论】:

    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    相关资源
    最近更新 更多