Heisenberg
Heisenberg

Reputation: 5299

How to extract from text data in pandas

I have txt file below

1a 2
3b45
 b 6
 c78

I would like to read and extract following the start position like below

structure=

start length
   1    1
   2    1
   3    2

I would like to read and extract dataframe like below

1   a   2 
3   b  45
nan b   6
nan c  78

and then,extract like below(key=2nd column,which contains 'b')

3   b   4  5
nan b  nan 6

and back to the original

3b45
 b 6

I tried 'read_fwf(widths=structure.length) But I couldnt figure out the next step.

How can I read and extract and merge?

Upvotes: 1

Views: 74

Answers (1)

su79eu7k
su79eu7k

Reputation: 7326

import pandas as pd

df = pd.read_table('sample.txt', header=None)
print(df)
df = df[0].str.extract('(\d*)(\D*\s*)(\d*)', expand=True).applymap(lambda x: x.strip())
print(df)
df = df[df[1].str.contains('b')]
print(df)
print(df[0] + df[1] + df[2])

      0
0  1a 2
1  3b45
2   b 6
3   c78

   0  1   2
0  1  a   2
1  3  b  45
2     b   6
3     c  78

   0  1   2
1  3  b  45
2     b   6

1    3b45
2      b6

Upvotes: 1

Related Questions