2017-10-28 67 views
-1

我有两列数据 (sample data),我想计算每周的一天的总用户数。我的代码不会产生任何输出 - Python

举例来说,我想这样我的输出(字典/列出什么都行):

星期一:25,周二 :30,周三 :45,周四 :50,周五 : 24,周六 :22,周日 :21

这里是我的尝试:

def rider_ship (filename): 
    with open('./data/Washington-2016-Summary.csv','r') as f_in: 

     Sdict = [] 
     Cdict = [] 
     reader = csv.DictReader(f_in) 
     for row in reader: 
      if row['user_type']=="Subscriber": 
       if row['day_of_week'] in Sdict: 
        Sdict[row['day_of_week']]+=1 
       else: 
        Sdict [row['day_of_week']] = row['day_of_week'] 
      else: 
       if row ['day_of_week'] in Cdict: 
        Cdict[row['day_of_week']] +=1 
       else: 
        Cdict[row['day_of_week']] = row['day_of_week'] 

     return Sdict, Cdict 

     print (Sdict) 
     print (Cdict) 

t= rider_ship ('./data/Washington-2016-Summary.csv') 
print (t) 

类型错误::列表索引必须为整数或切片,而不是str

+0

欢迎#1,请阅读[如何提问](https://stackoverflow.com/help/how-to-ask)。请特别注意[如何创建MCVE](https://stackoverflow.com/help/mcve)。您将付出更多努力发布一个好问题:一个容易阅读,理解和[主题](https://stackoverflow.com/help/on-topic) - 的机会更高会吸引相关人员,你会得到更快的帮助。祝你好运! – alfasin

+0

Sdict = {} Cdict = {}以 –

+0

开头好吧。完成后,我仍然遇到同样的错误。 –

回答

0

如何使用熊猫?

让我们先创建一个IO库中的文件对象:

import io 
s = u"""day_of_week,user_type 
Monday,subscriber 
Tuesday,customer 
Tuesday,subscriber 
Tuesday,subscriber""" 
file = io.StringIO(s) 

我们的实际代码:

import pandas as pd 
df = pd.read_csv(file) # "path/to/file.csv" 

Sdict = df[df["user_type"] == "subscriber"]["day_of_week"].value_counts().to_dict() 
Cdict = df[df["user_type"] == "customer"]["day_of_week"].value_counts().to_dict() 

现在我们有:

Sdict = {'Tuesday': 2, 'Monday': 1}

Cdict = {'Tuesday': 1}

+0

看起来不错。谢谢安东。 –

+0

@shreyaagarwal太棒了!考虑标记回答的问题。 –