I have a dataframe with 1 column containing the amount bought of a particular crypto
df['Amount'] = 200.20356AVAX
I would like to split this object into 2 columns:
- 1 containing the number –>
df['Quantity'] = 200.20356 - 1 containing the word –>
df['Asset'] = AVAX
>Solution :
Use str.extract:
df = pd.DataFrame({'Amount': ['200.20356AVAX']})
df = df.join(df['Amount'].str.extract('([^A-Z]+)([A-Z]+)') \
.rename(columns={0: 'Quantity', 1: 'Asset'}))
# OR, proposed by @mozway (more efficient)
df = df.join(df['Amount'].str.extract('(?P<Quantity>[^A-Z]+)(?P<Asset>[A-Z]+)'))
Output:
>>> df
Amount Quantity Asset
0 200.20356AVAX 200.20356 AVAX