将键值对读入Pandas

时间:2013-04-09 16:52:55

标签: python pandas

Pandas让您可以轻松阅读CSV文件:

pd.read_table('data.txt', sep=',')

Pandas对于具有键值对的文件有类似的东西吗?我想到了这个:

pd.DataFrame([dict([p.split('=') for p in l.split(',')]) for l in open('data.txt')])

如果不是内置的,那么可能是更惯用的东西?

感兴趣的文件如下所示:

symbol=ESM3,exchange=GLOBEX,timestamp=1365428525690751,price=1548.00,quantity=551
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525697183,price=1548.00,quantity=551
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525714498,price=1548.00,quantity=551
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525734967,price=1548.00,quantity=551
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525735567,price=1548.00,quantity=555
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525735585,price=1548.00,quantity=556
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525736116,price=1548.00,quantity=556
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525740757,price=1548.00,quantity=556
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525748502,price=1548.00,quantity=556
symbol=ESM3,exchange=GLOBEX,timestamp=1365428525748952,price=1548.00,quantity=557

每行都有完全相同的键,顺序相同。没有空值。要生成的表是:

  exchange    price quantity symbol         timestamp
0   GLOBEX  1548.00    551\n   ESM3  1365428525690751
1   GLOBEX  1548.00    551\n   ESM3  1365428525697183
2   GLOBEX  1548.00    551\n   ESM3  1365428525714498
3   GLOBEX  1548.00    551\n   ESM3  1365428525734967
4   GLOBEX  1548.00    555\n   ESM3  1365428525735567
5   GLOBEX  1548.00    556\n   ESM3  1365428525735585
6   GLOBEX  1548.00    556\n   ESM3  1365428525736116
7   GLOBEX  1548.00    556\n   ESM3  1365428525740757
8   GLOBEX  1548.00    556\n   ESM3  1365428525748502
9   GLOBEX  1548.00    557\n   ESM3  1365428525748952

(我可以在我带入\n之后将quantity移除rstrip()。)

2 个答案:

答案 0 :(得分:4)

如果事先知道了密钥名称,并且名称总是以相同的顺序出现,那么您可以使用转换器来删除密钥名称,然后使用names参数来命名列:< / p>

import pandas as pd

def value(item):
    return item[item.find('=')+1:]

df = pd.read_table('data.txt', header=None, delimiter=',',
                   converters={i:value for i in range(5)},
                   names='symbol exchange timestamp price quantity'.split())
print(df)

发布的数据产量

  symbol exchange         timestamp    price quantity
0   ESM3   GLOBEX  1365428525690751  1548.00      551
1   ESM3   GLOBEX  1365428525697183  1548.00      551
2   ESM3   GLOBEX  1365428525714498  1548.00      551
3   ESM3   GLOBEX  1365428525734967  1548.00      551
4   ESM3   GLOBEX  1365428525735567  1548.00      555
5   ESM3   GLOBEX  1365428525735585  1548.00      556
6   ESM3   GLOBEX  1365428525736116  1548.00      556
7   ESM3   GLOBEX  1365428525740757  1548.00      556
8   ESM3   GLOBEX  1365428525748502  1548.00      556
9   ESM3   GLOBEX  1365428525748952  1548.00      557

答案 1 :(得分:2)

我不确定这样做的最佳方法是什么,但假设在值中没有找到分隔符 - 这会让我的大脑想到角落的情况 - 那么这样的事情就不是了超级优雅,但很简单:

>>> df = pd.read_csv("esm.csv", sep=",|=", header=None)
>>> df2 = df.ix[:,1::2]
>>> df2.columns = list(df.ix[0,0::2])
>>> df2
  symbol exchange         timestamp  price  quantity
0   ESM3   GLOBEX  1365428525690751   1548       551
1   ESM3   GLOBEX  1365428525697183   1548       551
2   ESM3   GLOBEX  1365428525714498   1548       551
3   ESM3   GLOBEX  1365428525734967   1548       551
4   ESM3   GLOBEX  1365428525735567   1548       555
5   ESM3   GLOBEX  1365428525735585   1548       556
6   ESM3   GLOBEX  1365428525736116   1548       556
7   ESM3   GLOBEX  1365428525740757   1548       556
8   ESM3   GLOBEX  1365428525748502   1548       556
9   ESM3   GLOBEX  1365428525748952   1548       557

基本上,读取它,然后自己动作,保留其他所有元素,然后修复列名。