Nono_sad
Nono_sad

Reputation: 443

Pandas extract str in column with regex

I have a dataframe, in one column I want to extract a specific information. Using split it can be done easily but with Pandas I cannot figure out how to do it.

s = pd.Series(['T:15.0(1.71%),B:7.4(0.03%),P:1e-21'])

I want to extract only 1e-21

I tried

s.str.extract()

But I cannot find the regex to match what I want actually.

Or if there is another way to parse column content.

Thanks

Upvotes: 0

Views: 72

Answers (3)

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

Try:

print(s.str.extract(r"P:([^\,]+)"))

This will return whatever you have after literal: P: which isn't comma (since I understand comma terminates assignment).

Upvotes: 1

NYC Coder
NYC Coder

Reputation: 7594

You can try this:

print(s.str.extract('(\d+e-\d+)'))

       0
0  1e-21

Upvotes: 2

bigbounty
bigbounty

Reputation: 17368

s = pd.Series(['T:15.0(1.71%),B:7.4(0.03%),P:1e-21'])
print(s.str.split(":")[0][-1])

Output:

'1e-21'

Upvotes: 1

Related Questions