数据帧列在pandas中转置

时间:2016-02-06 20:06:47

标签: python pandas

我的源文件中的数据格式为

Open,0.001
High,0.001
Low,0.001
Close,0.001
Volume,0
Adj Close,0.001
SMA_20,0.0010000000000000005
SMA_50,0.0010000000000000007
date_of_trade,2016-02-05 00:00:00
code,AFT
Open,2.9300000000000002
High,2.9700000000000002
Low,2.8300000000000001
Close,2.8999999999999999
Volume,631100
Adj Close,2.8999999999999999
SMA_20,3.2214999999999998
SMA_50,3.0767999999999969
date_of_trade,2016-02-05 00:00:00
code,1PG
Open,6.9900000000000002
High,7.0999999999999996
Low,6.9000000000000004
Close,6.9000000000000004
Volume,4300

我想转换成以下格式

open,high,low,close,volume,adj_close,sma_20,sma_50,data_of_trade,code
.001,.001,.001,0,.001,.0001000005,.0000100007,2016-02-05 00:00:00,aft
2.93,2.97,.......................................................,1pg

源文件中的第一列是列名,第二列是相应的列值。

我试过枢轴等,但无法使它工作。欢迎任何帮助。

2 个答案:

答案 0 :(得分:3)

要使pivot正常工作,您需要为每条记录添加唯一标识符,例如如果原始数据中的1条记录长度为10行,那么行号的整数除以10将为:

df = pd.read_csv(data, header=None, names=["key", "value"])
df["index"] = [i // 10 for i in range(len(df))]
df = df.pivot("index", "key", "value")

FTR:如果你有超过几十亿条记录(>你的RAM的免费GB),那么使用http://pandas.pydata.org/pandas-docs/stable/io.html#io-chunking 功能

答案 1 :(得分:1)

我认为你真的不需要熊猫机械。你可以手工完成换位:

from csv import DictWriter
from toolz import partition

cols = 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close', 'SMA_20', 'SMA_50', 'date_of_trade', 'code'

with open('old.csv') as old, open('new.csv', 'w') as new:
    writer = DictWriter(new, cols)
    writer.writeheader()

    for lines in partition(len(cols), old):
        writer.writerow(dict(l.strip().split(',') for l in lines))