简体   繁体   English

如何在Python中将列的第一个元素与CSV文件中的其他元素进行比较?

[英]How to compare first element of a column with other elements in CSV file in Python?

I want to compare first element of a column with other elements of same column and next column and print element which is present once only in both the columns as well as value in its corresponding column. 我想将一列的第一个元素与同一列和下一列的其他元素以及打印元素进行比较,该元素仅在两个列中都存在一次,并且在其对应列中也存在一次值。 For eg: 例如:

Col1 Col2 Col1 Col2
111 243 111243
111 145 111145
213 111 213111
289 200 289200
222 213 222213

In this example 243 is present only once but 111(value in corresponding column) is present more then once. 在此示例中,243仅出现一次,而111(对应列中的值)则出现一次以上。 I want to print only 我只想打印

289 200 289200

My code is: 我的代码是:

import csv
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
    pass
f = open("1.csv", 'r')
reader = csv.reader(f)
a = []
b = []
c = []
for row in reader:
    a.append(row[0])
    b.append(row[1])
val = [i for i,j in OrderedCounter(a).items() if j==1]
val1 = [i for i,j in OrderedCounter(b).items() if j==1]

It is giving me the unique values of each column 它给我每一列的唯一值

from collections import Counter    
import csv

values = Counter()
data = {}

with open('1.csv') as f_input:
    csv_input = csv.reader(f_input)
    header = next(csv_input)    # comment out if no actual header

    for row in csv_input:
        values[row[0]] += 1
        values[row[1]] += 1
        data[row[0]] = row[1]

for value, count in values.items():
    try:
        if count == 1 and values[data[value]] == 1:
            print '{} {}'.format(value, data[value])
    except KeyError as e:
        pass

This would display: 这将显示:

289  200

暂无
暂无

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

相关问题 Python:如何相对于除第一列以外的列对csv文件进行切片? - Python: how to slice a csv file with respect to a column other than the first? 如何在python中传递csv文件的第一列 - How to pass the first column of a csv file in python 使用python将CSV中的每个元素与其他元素进行比较 - Compare each element in CSV with other using python 如何比较python中csv文件的列中的值? - How do I compare values within a column in a csv file in python? 如何将csv文件的一列与python中的图像名称进行比较? - How to compare a column of a csv file with an image name in python? 在Python中,如何根据一列中的值比较两个csv文件并从第一个文件中输出与第二个不匹配的记录 - In Python, how to compare two csv files based on values in one column and output records from first file that do not match second 在Python中的csv文件中比较一列到另一列 - compare one column to another in csv file in Python 如果在Python中发现重复项,如何使用列表中的某些元素比较其他列表中的元素并输出某个元素? - How to use certain elements in a list to compare elements in other lists and output a certain element if duplicates are found in Python? 如何使用python比较列表中的元素并检查第一个列表元素是否包含在另一个列表的元素中 - How to compare elements in lists and check if first list element contains in another list's element using python 如何在使用 python 时只对 csv 文件中的第一列进行排序而不影响任何其他列 - How do you sort only the first column in a csv file and not affect any of the other columns while doing so using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM