'How to split integers that contains strings, such as 73101P
import pandas as pd
d = {'element': ['COGS', 'Other current assets', 'COGS','COGS', 'Other current assets', 'COGS'], 'account_number': ['5721-ES', '1101', '5721-ESP', '73101L', '73230K', '11106' ]}
df = pd.DataFrame(data=d)
df
I need only the numbers without letters for converting them to numeric values afterwards.
However, I can't split these integers for instance 73101K
df.account_number = df.account_number.apply(lambda x: x.split('-')[0])
Solution 1:[1]
You can use findall() that finds every digit (\d) and then join it together afterwards:
df["account_number"] = (
df["account_number"].str.findall(r"\d").str.join(sep="").astype(int)
)
print(df)
Prints:
element account_number
0 COGS 5721
1 Other current assets 1101
2 COGS 5721
3 COGS 73101
4 Other current assets 73230
5 COGS 11106
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
