Reading key-value pairs into Pandas

后端 未结 2 1370
终归单人心
终归单人心 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:35

    I'm not sure what the best way to do this is, but assuming that the delimiters aren't found in the values -- it hurts my brain to think of the corner cases -- then something like this isn't super-elegant but is straightforward:

    >>> 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
    

    Basically, read it in, and then do the pivot yourself, keeping every other element and then fixing the column names.

提交回复
热议问题