Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

python - Group data from a CSV file by field value

I have a csv file which has duplicate value in first column . I want to collect all value of second column in a list for one value of first column

column1 column2
a        54.2
s        78.5
k        89.62
a        77.2
a        65.56

I want to get like

print a  # [54.2,77.2,65.56]
print s  # [78.5]
print k  # [89.62]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems fairly straightforward using python's CSV reader.

data.csv

a,54.2
s,78.5
k,89.62
a,77.2
a,65.56

script.py

import csv

result = {}

with open('data.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in csvreader:
        if row[0] in result:
            result[row[0]].append(row[1])
        else:
            result[row[0]] = [row[1]]

print result

output

{
    'a': ['54.2', '77.2', '65.56'], 
    's': ['78.5'], 
    'k': ['89.62']
}

As @Pete poined out, you can beautify it using defaultdict:

script.py

import csv
from collections import defaultdict

result = defaultdict(list)  # each entry of the dict is, by default, an empty list

with open('data.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in csvreader:
        result[row[0]].append(row[1])

print result

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...