Reading key-value pairs into Pandas

后端 未结 2 1366
终归单人心
终归单人心 2021-01-15 10:46

Pandas makes it really easy to read a CSV file:

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

Does Pandas having something similar for a file with

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-15 11:42

    If you know the key names beforehand and if the names always appear in the same order, then you could use a converter to chop off the key names, and then use the names parameter to name the columns:

    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)
    

    on your posted data yields

      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
    

提交回复
热议问题