2017-02-16 7 views
0

Pythonの新機能です。調査の結果を得るには何か助けが必要です。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 

私は別の答えは、人と性別ごとの発生回数をカウントしたい:私はこのようになりますCSVファイルを、持っています。たとえば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)) 

しかし、私は文字列のみを持っているので、私はどんな結果を得ることはありませんと思います。さらに、私はまだ人/性別でデータを取得する方法を知らない。 ありがとうございます!

+1

これは['pandas'](http://pandas.pydata.org/pandas-docs/stable/10min.html)を使用すると非常に簡単になります – Peter9192

答えて

1

あなたが何か行うことができ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() 
+0

ありがとうございます。出来た! – Carro

+0

ありがとうございました。それでも私はもう一つ質問があります。 Groupby.applyを使用してすべての質問の回答を得ることは可能ですか?たとえば、grouped = data.groupby(['people'、 'gender'])['Q1'、 'Q2'、...]と書いたり、それを行う他の方法はありますか? – Carro

+0

おそらくすべての1つの質問を通過するループですか? – Carro

0

を、あなたはそれを読んだ後の行に解析のビットを行う必要があります。次のようなもの(未テスト)。これは、(まだ)存在しないキーを参照することが自動的に作成され、値0を与えるという点を除いて、普通の辞書とよく似たCounterオブジェクトを使用します。KeyErrorを上げるのではなく、値0を与えます。

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ファイルの最初の行は、いくつかの列ヘッダである場合は、事前にそれを読んで、その後、カウンタのリストに注釈を付けるためにそれを使用することができます。私はカウンタオブジェクトの生のダンプがあまりにも醜いと見なされるべきならば、運動のようにカウンターの内容をあなたにきれいに書式設定しておきます。

関連する問題