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])
CodePudding user response:
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