Python CSV 文件的计数器
Python counter for a CSV file
我是 Python 的新手,我需要一些帮助才能获得调查结果。我有一个 CSV 文件,如下所示:
Person, Gender, Q1, Q2, Q3
professor, male, agree, not agree, agree
professor, male, agree, agree, agree
professor, female, neutral, not agree, agree
Professor, female, agree, agree, agree
student, female, agree, not agree, not agree
student, female, no answer, not agree, agree
student, male, no answer, no answer, agree
我想计算每个人和性别出现不同答案的次数。比如Q1:(教授,男:同意,2),(教授,女:同意1;中立1)等等。
到目前为止我已经试过了:
import csv
from collections import Counter
with open('survey.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',', dialect = csv.excel_tab)
counts = Counter(map(tuple,reader))
print [row for row in reader if row]
print list(csv.reader(csvfile))
但我想因为我只有字符串,所以我没有得到任何结果。而且,我还不知道如何通过people/gender获取数据。
非常感谢!
使用 pandas
你可以这样做:
import pandas as pd
my_data = pd.read_csv('survey.csv')
# To summarize the dataframe for everything together:
print my_data.describe()
print my_data.sum()
# To group by gender, etc.
my_data.groupby('Gender').count()
如果你不想切换到pandas,你需要在阅读它们之后对行进行一些分析。类似于以下内容(未经测试)。这使用了 Counter objects ,它的行为很像普通的字典,除了引用一个(还)不存在的键会自动创建它并赋予它值 0,而不是提高 KeyError
.
from collections import Counter
counters = []
for row in reader:
for colno,datum in enumerate(row):
if colno >= len(counters): # do we have a counter for this column yet?
counters.append( Counter() ) # if not, add another Counter
counters[colno][datum] += 1
for counter in counters:
print(counter)
如果你的csv文件的第一行是某列headers,你可以提前阅读它,然后用它来注释计数器列表。如果计数器 objects 的原始转储被认为太难看,我将把计数器内容的格式作为练习留给你。
我是 Python 的新手,我需要一些帮助才能获得调查结果。我有一个 CSV 文件,如下所示:
Person, Gender, Q1, Q2, Q3
professor, male, agree, not agree, agree
professor, male, agree, agree, agree
professor, female, neutral, not agree, agree
Professor, female, agree, agree, agree
student, female, agree, not agree, not agree
student, female, no answer, not agree, agree
student, male, no answer, no answer, agree
我想计算每个人和性别出现不同答案的次数。比如Q1:(教授,男:同意,2),(教授,女:同意1;中立1)等等。 到目前为止我已经试过了:
import csv
from collections import Counter
with open('survey.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',', dialect = csv.excel_tab)
counts = Counter(map(tuple,reader))
print [row for row in reader if row]
print list(csv.reader(csvfile))
但我想因为我只有字符串,所以我没有得到任何结果。而且,我还不知道如何通过people/gender获取数据。 非常感谢!
使用 pandas
你可以这样做:
import pandas as pd
my_data = pd.read_csv('survey.csv')
# To summarize the dataframe for everything together:
print my_data.describe()
print my_data.sum()
# To group by gender, etc.
my_data.groupby('Gender').count()
如果你不想切换到pandas,你需要在阅读它们之后对行进行一些分析。类似于以下内容(未经测试)。这使用了 Counter objects ,它的行为很像普通的字典,除了引用一个(还)不存在的键会自动创建它并赋予它值 0,而不是提高 KeyError
.
from collections import Counter
counters = []
for row in reader:
for colno,datum in enumerate(row):
if colno >= len(counters): # do we have a counter for this column yet?
counters.append( Counter() ) # if not, add another Counter
counters[colno][datum] += 1
for counter in counters:
print(counter)
如果你的csv文件的第一行是某列headers,你可以提前阅读它,然后用它来注释计数器列表。如果计数器 objects 的原始转储被认为太难看,我将把计数器内容的格式作为练习留给你。