如何使用现有数据在 CSV 文件中创建新列,然后将其用作打印排序列表的键

对于作业,我需要从 CSV 文件中按日期顺序生成投注列表。文件如下(样本):


Aintree, Red Rum,2017,5,12,11.58, won

Aintree, Hurricane Fly,2017,5,12,11.58, won

Aintree, Murder,2017,5,12,5, lost

Ayr, Corbiere,2016,11,3,25, lost

我想为在 CSV 中[2],[3],[4]以格式组合的每一行创建一个新列'%d-%b-%y'。然后,我需要使用这个新列作为关键,以[5]按日期顺序生成投注列表。


我在下面有这段代码,我是 python 的新手,我没有取得太大的成功,也不知道哪里出了问题。运行时会产生此错误:


Traceback (most recent call last):

  File "date_bet.py", line 25, in <module>

    get_date()

  File "date_bet.py", line 10, in get_date

    data = list(csv.reader(csvFile))

io.UnsupportedOperation: not readable

代码:


import csv

from datetime import datetime


def get_date():

    with open('results.csv', 'a') as csvFile:

        writer = csv.writer(csvFile)

        reader = csv.reader(csvFile)


        all = []

        data = list(csv.reader(csvFile))

        row = next(csvFile)

        row.append([7])

        all.append(row)


    for row in data:

        row.append((data[4],data[3],data[2]), '%d-%b-%y')

        all.append(row)


    writer.writerows(all)



    date = datetime.date(row[7], '%x')

    print(row[5], key = date)


get_date() 


莫回无
浏览 127回答 2
2回答

智慧大石

也许您应该考虑使用熊猫来实现您想要的。我想以'%d-%b-%y' 格式为结合2 , 3 ,[4] 的每一行创建一个新列import pandas as pddf = pd.read_csv(<your_file_name>, header=None)使用pd.to_datetime函数添加列:df['date'] = pd.to_datetime(dict(year=df[2], month=df[3], day=df[4]), format='%d-%b-%y')df&nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp;4&nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; &nbsp;6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;date0&nbsp; &nbsp;Aintree&nbsp; &nbsp; &nbsp;Red Rum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2017&nbsp; &nbsp; 5&nbsp; &nbsp;12&nbsp; 11.58&nbsp; &nbsp;won&nbsp; &nbsp; &nbsp;2017-05-121&nbsp; &nbsp;Aintree&nbsp; &nbsp; &nbsp;Hurricane Fly&nbsp; &nbsp;2017&nbsp; &nbsp; 5&nbsp; &nbsp;12&nbsp; 11.58&nbsp; &nbsp;won&nbsp; &nbsp; &nbsp;2017-05-122&nbsp; &nbsp;Aintree&nbsp; &nbsp; &nbsp;Murder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2017&nbsp; &nbsp; 5&nbsp; &nbsp;12&nbsp; 5.00&nbsp; &nbsp; lost&nbsp; &nbsp; 2017-05-123&nbsp; &nbsp;Ayr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Corbiere&nbsp; &nbsp; &nbsp; &nbsp; 2016&nbsp; &nbsp; 11&nbsp; 3&nbsp; &nbsp;25.00&nbsp; &nbsp;lost&nbsp; &nbsp; 2016-11-03然后,我需要使用这个新列作为关键,以按日期顺序生成投注列表 [5]。df.sort_values('date', inplace=True)print(df[[5, 'date']].reset_index(drop=True)) # reset index so that you don't see the mixedup index.&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; &nbsp;date0&nbsp; 25.00 2016-11-031&nbsp; 11.58 2017-05-122&nbsp; 11.58 2017-05-123&nbsp; &nbsp;5.00 2017-05-12您可以使用df.to_csv()方法将其保存到 csv 。

慕哥6287543

使用熊猫。添加名为“日期”的新列并按新列对数据进行排序。import pandas as pddf = pd.read_csv('55611308.csv', sep=',', names=['name0', 'name1', 'y', 'm', 'd', 'h', 'result'], header=None)df['date'] = df['y'].astype(str).str.cat(df['m'].astype(str), sep='-').str.cat(df['d'].astype(str), sep='-')df.sort_values('date',inplace=True)print(df)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python