Home > database >  Replacing strings in one column with the details of another column in a single dataframe
Replacing strings in one column with the details of another column in a single dataframe

Time:01-30

I need to be able to replace values in one column from another column in a single dataframe

enter image description here

I imported this excel file as a pandas dataframe but how do I replace the values in left column (Freedom Town) with the values in the right column that come before the hyphen ( Fluent) in a pandas dataframe?

CodePudding user response:

You can use str.split to split on ' - ' then keep only the second part:

# replace colA & colB by real column names
df['colA'] = df['colB'].str.split(' - ').str[1]

CodePudding user response:

Use Series.str.extract for values after -:

df['colA'] = df['colB'].str.extract('\s -\s (.*)', expand=False)
  • Related