How to split key, value from text file using pandas?

纵然是瞬间 提交于 2020-01-05 04:10:07

问题


I'm having input text file like this :

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

I want to split this key and value pairs and showing in the format like this :

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

see In the index[0] 6 and 9 th record's values are Blank because 6 is available in the other column but not in this. Like this in index[1] 5th record is Blank.

Program-Code-

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

I'm getting this output :

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

I'm getting only that records which are available in the input text files. How can I keep record value as a Blank?


回答1:


you can do it using .str.extract() function in conjunction with a generated 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)

Test:

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


来源:https://stackoverflow.com/questions/38371440/how-to-split-key-value-from-text-file-using-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!