CSV文件的Python计数器
问题描述:
我是Python新手,需要一些帮助才能获得调查结果。我有一个CSV文件,它看起来像这样:CSV文件的Python计数器
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))
但我觉得,因为我只有串,我没有得到任何结果。而且,我还不知道如何通过人/性别获取数据。 非常感谢!
答
使用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()
答
如果你不想切换到大熊猫,你需要做一些分析,对行,你看他们之后。像下面这样(未经测试)。这使用Counter对象,其行为与普通的字典非常相似,除了引用不存在(但不存在)的键自动创建并赋予其值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文件的第一行是一些列标题,您可以提前阅读它,然后用它来诠释计数器的列表。如果原材料堆放的柜台物品被认为太难看,我会尽量将柜台的内容格式化为你的练习。
使用['pandas'](http://pandas.pydata.org/pandas-docs/stable/10min.html) – Peter9192