Mr Paul
Mr Paul

Reputation: 73

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

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

Answers (2)

Kyle Dixon
Kyle Dixon

Reputation: 494

If I'm understanding correctly, your current dataframe looks something like this:

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

Upvotes: 1

nrameyka
nrameyka

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

Related Questions