Home > Blockchain >  python pandas , create a new column from an existing column , take the x n umber of characters from
python pandas , create a new column from an existing column , take the x n umber of characters from

Time:10-23

i have a dataframe:

Project


The Bike Shop - London

Car Dealer - New York

Airport - Berlin

I want to add 2 new columns to the dataframe : business & location.

i can find where the "-" is in the string by using: df['separator'] = df['Project'].str.find('-')

whats the best and cleanest way to get 2 new fields into the dataframe? ie, ProjectType & Location

Project ProjectType Location


The Bike Shop - London the Bike Shop London

Car Dealer - New York Car Dealer New York

Airport - Berlin Airport Berlin

thanks in advance :)

CodePudding user response:

if your data is separated by '-', you can split it into several columns at once

new_df = df['new_values'].str.split('\n',expand=True)

here it is well described how to divide the column into others enter image description here

and you want it to look like this:

enter image description here

If that's what you're looking for, you can use a list comprehension:

df['ProjectType'] = [project.split(' - ')[0] for project in df['Project']]
df['Location'] = [project.split(' - ')[1] for project in df['Project']]
del df['Project'] # If you want to remove the original column
  • Related