search

Home  >  Q&A  >  body text

python 读取csv文件中某一段月份中的数据?

这是问题:Find out how much time and over how many separate days did R.Lennon work on administering the Jira Server between August and September.

csv里是这样的

我才开始学习timeseries部分,大概逻辑有了,但是不知道怎么用代码表达。请大神帮忙提一点建议,谢谢!

迷茫迷茫2824 days ago1022

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-18 10:18:45

    Split Date field using re.split

    import csv,re
    
    with open('xxx.csv','rb') as rf:
        reader = csv.reader(rf)
        with open('xxx_new.csv','wb') as wf:
            writer = csv.writer(wf)
            headers = reader.next()
            writer.writerow(headers)
            for row in reader:
                t = re.split('\W+',row[1])  
                # row[1]为Date字段,被拆为['1', '11', '2016', '14', '17']
                if int(t[1]) == 11:  # 假设你想要11月数据
                    writer.writerow(row)

    reply
    0
  • 阿神

    阿神2017-04-18 10:18:45

    You said timeseries, did you use pandas?
    If it’s pandas, it’s actually quite simple. Assume that the name of datefrmae is df
    First make sure that the Date column is converted to DatetimeIndex. This can be done with df['newdate']=pd.DatetimeIndex(df['date'])
    Then filter df[df['newdate' ].dt.month==9] can filter out all September data,

    reply
    0
  • Cancelreply