How To Split Key, Value From Text File Using Pandas?
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 s
Solution 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 intervalfor i inrange(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
01=88|2=1438|3=KKK|4=7.7|5=00|7=66|8=a
11=13|2=1388|3=DDD|4=157.73|6=00|7=08|8=b|9=k
21=11|3=33|5=55In [179]: newOut[179]:
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11
0881438 KKK 7.70066 a
1131388 DDD 157.730008 b k
2113355
Post a Comment for "How To Split Key, Value From Text File Using Pandas?"