简体   繁体   中英

How Can I Extract a Specific Number From a Column With Text

Hi, I have a dataset with a column with text and multiple numbers and I want to extract a specific number from the column and create a new one with it.

Deposit Generated By Sale Of Foreign Currency From Client 165.22 USD at an exchange rate of ** 19.650000 **

Thats the text I have on each row of that column and im only interested on the exchange rate. Another problem is that not every row has it so when there's no exchange rate on that row I'd like to use a number from another column that is already set as float

You could use str.extract with a capture group:

df["curr"] = df["text"].str.extract(r'(\d+(?:\.\d+)?) [A-Z]{2,}')

Here is a regex demo showing that the logic is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM