透视csv数据?

时间:2016-12-27 16:05:17

标签: python python-3.x csv pivot

我是Python的新手,所以如果这很容易请原谅。我有一个包含以下数据的csv文件:

Symbol,date,price
apple, 23/12/2016, 50
apple, 26/12/2016, 51
apple, 27/12/2016, 52
google,23/12/2016, 70
google,26/12/2016, 71
google,27/12/2016, 72

我需要编写一个新的csv文件,如下所示:

Date, apple, google
23/12/2016,50,70
26/12/2016,51,71
27/12/2016,52,72

我到目前为止的代码如下。但是,我似乎无法将符号作为列。

import csv
import os

csv.register_dialect(
    'mydialect',
    delimiter =',',
    )

symbol, date=[],[]

with open('EB_CUT2.csv',"rt") as dfile:
    thedata = csv.reader(dfile, dialect ='mydialect')
    for row in thedata:
        if row[4] not in date:
            date.append(row[4])
        if row[0] not in symbol:
            symbol.append(row[0])
dfile.close()

ebout = open('EB_CUT.csv',"wt",newline='')
wr = csv.writer(ebout)
for val in date:
    wr.writerow([val])
ebout.close()

2 个答案:

答案 0 :(得分:0)

熊猫解决方案:

import pandas 

df = pd.read_csv('EB_CUT2.csv', index_col=1, parse_dates=True)
df2 = pd.pivot_table(df, values='price', columns=['Symbol'], index=df.index)
df2.to_csv('EB_CUT.csv')

EB_CUT.csv的内容:

date,apple,google
2016-12-23,50,70
2016-12-26,51,71
2016-12-27,52,72

答案 1 :(得分:0)

非熊猫解决方案:

import csv
from datetime import datetime
import os

MY_DIALECT = 'mydialect'
csv.register_dialect(MY_DIALECT, delimiter =',', skipinitialspace=True,)

daily_prices = {}
with open('EB_CUT2.csv', "rt", newline='') as dfile:
    reader = csv.DictReader(dfile, dialect=MY_DIALECT)
    for row in reader:
        date = datetime.date(datetime.strptime(row['date'], '%d/%m/%Y'))
        entry = daily_prices.setdefault(date, {})
        entry[row['Symbol']] = row['price']

all_symbols = sorted(
    set(symbol for value in daily_prices.values() for symbol in value))

with open('EB_CUT.csv', "wt", newline='') as ebout:
    writer = csv.writer(ebout)
    writer.writerow(['Date'] + all_symbols) # header
    for date, prices in sorted(daily_prices.items()):
        row = [date.strftime('%d/%m/%Y')]
        for symbol in all_symbols:
            row.append(prices.get(symbol, ''))
        writer.writerow(row)

如果你想从任何现有的daily_prices文件初始化'EB_CUT.csv'字典 - 实际上是上面显示的最后一步的反转 - 听起来你可能会从下面的评论中想要合并稍后会有更多csv文件,以下代码会这样做:

daily_prices = {}
if os.path.isfile('EB_CUT.csv'):  # existing file?
    # Initialize daily_prices from existing csv file.
    with open('EB_CUT.csv', "rt", newline='') as inf:
        reader = csv.reader(inf)
        next(reader)  # skip header row
        for row in reader:
            date = datetime.date(datetime.strptime(row[0], '%d/%m/%Y'))
            days_prices = daily_prices.setdefault(date, {})
            for symbol, price in row[1:]:
                days_prices[symbol] = price

读取当前csv文件以初始化它的另一种方法是始终将以当前csv文件对应的daily_prices数据保存在您选择的任何格式的单独文件中。可以使用picklejson模块轻松地编写和回读它。