如何使用pandas从文本文件中拆分键值?

时间:2016-07-14 10:12:48

标签: python pandas dataframe key key-value

我有这样的输入文本文件:

INPUT.TXT -

1=88|2=1438|3=KKK|4=7.7|5=00|7=66|8=a
1=13|2=1388|3=DDD|4=157.73|6=00|7=08|8=b|9=k

我想拆分这个键和值对,并以这样的格式显示:

Output.txt的 -

index[0]
1     88
2     1438
3     kkk
4     7.7
5     00
6     
7     66
8     a
9    

index[1]
1     13
2     1438
3     DDD
4     157.73
5    
6     00
7     08
8     b
9     k

参见索引[0]中的第6和第9个记录的值为空,因为其他列中有6个可用但不在此列中。在索引[1]中就像这样,第5条记录为空白。

程序编码 -

df = pd.read_csv(inputfile, index_col=None, names=['text'])

    #spliting two times with respect to (= & |) and saving into stack
    s = df.text.str.split('|', expand=True).stack().str.split('=', expand=True)

    #giving index's as empty string ('') i.e. for removing
    s.columns = ['idx','']

    #rename_axis(None) for excluding index values  
    dfs = [g.set_index('idx').rename_axis(None) for i, g in s.groupby(level=0)]

    #length for iterating through list
    dfs_length = len(dfs)


    #opening output file
    with open(outputfile + 'output.txt','w') as file_obj:
        i = 0
        while i < dfs_length:
            #index of each column
            s = '\nindex[%d]\n'%i
            #writing index to file
            file_obj.write(str(s))
            #print '\nindex[%d]'%i
            #print dfs[i]
            #wriring actual contents to file
            file_obj.write(str(dfs[i])+'\n')
            i = i + 1

我收到了这个输出:

output.txt的 -

index[0]
1     88
2     1438
3     kkk
4     7.7
5     00
7     66
8     a

index[1]
1     13
2     1438
3     DDD
4     157.73
6     00
7     08
8     b
9     k

我只获得输入文本文件中可用的记录。如何将记录值保留为空白?

1 个答案:

答案 0 :(得分:0)

您可以使用.str.extract()函数与生成的RegEx结合使用

pat = r'(?:1=)?(?P<a1>[^\|]*)?'

# you may want to adjust the right bound of the range interval
for i in range(2, 12):
    pat += r'(?:\|{0}=)?(?P<a{0}>[^\|]*)?'.format(i)

new = df.val.str.extract(pat, expand=True)

测试:

In [178]: df
Out[178]:
                                            val
0         1=88|2=1438|3=KKK|4=7.7|5=00|7=66|8=a
1  1=13|2=1388|3=DDD|4=157.73|6=00|7=08|8=b|9=k
2                                1=11|3=33|5=55

In [179]: new
Out[179]:
   a1    a2   a3      a4  a5  a6  a7 a8 a9 a10 a11
0  88  1438  KKK     7.7  00      66  a
1  13  1388  DDD  157.73      00  08  b  k
2  11         33          55