Reputation: 73
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 :)
Upvotes: 1
Views: 139
Reputation: 494
If I'm understanding correctly, your current dataframe looks something like this:
and you want it to look like this:
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
Upvotes: 1
Reputation: 63
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 http://datalytics.ru/all/kak-v-pandas-razbit-kolonku-na-neskolko-kolonok/
Upvotes: 1