Home > Software engineering >  Split the prices from string and place it in another column in Pandas
Split the prices from string and place it in another column in Pandas

Time:12-15

I have below dataframe:

Charges:
Shipping:800.00
Processing:9000.00
Commission:150.00

What I'm trying to do is to split charge amounts separately to different column:

Charges:     Prices:
Shipping     800.00
Processing   9000.00
Commission   150.00

Tried to use regex and df['Prices'] = df['Charges'].str.split('[0 9]', expand=True) with no success.

Any suggestions:

CodePudding user response:

With split:

splitted = df['Charges'].str.split(':', expand=True)
df['Charges'] = splitted[0]
df['Price'] = splitted[1]

CodePudding user response:

We can use str.extract here:

df["Prices"] = df["Charges"].str.extract(r'(\d (?:\.\d ))')
df["Charges"] = df["Charges"].str.extract(r'^([^:] )')
  • Related