toerag
toerag

Reputation: 59

Pandas : Remove a part of a string before the first white space

Good morning, I have a data frame:

df1 = pd.DataFrame({'Item_Description': ['dfd_landing BKK_AOT-Airbridge / Jetty', 'dfd_month DEL_Updater Services Pvt Ltd-Office Cleaning'])

I would like to remove everything before the first space so the result would be:

df1 = pd.DataFrame({'Item_Description': ['BKK_AOT-Airbridge / Jetty', 'DEL_Updater Services Pvt Ltd-Office Cleaning'])

I tried with :

    df1['Item_Description'] = df1['Item_Description'].str.replace(".*(" ")", "")

but it removes everything.

Is there any way to do it in one line?

Kind regards

Upvotes: 1

Views: 1498

Answers (3)

Naveed
Naveed

Reputation: 11650

here is another way to do it using regex

df1['item'] = df1['Item_Description'].str.extract(r'\s(.*)')
df1
    Item_Description                                    item
0   dfd_landing BKK_AOT-Airbridge / Jetty               BKK_AOT-Airbridge / Jetty
1   dfd_month DEL_Updater Services Pvt Ltd-Office ...   DEL_Updater Services Pvt Ltd-Office Cleaning

Upvotes: 0

otavios
otavios

Reputation: 124

You can also split and join without apply:

import pandas as pd

df1 = pd.DataFrame({'Item_Description': ['dfd_landing BKK_AOT-Airbridge / Jetty', 'dfd_month DEL_Updater Services Pvt Ltd-Office Cleaning']})

df1['Item_Description'] = df1['Item_Description'].str.split().str[1:].str.join(' ')

Upvotes: 3

ArchAngelPwn
ArchAngelPwn

Reputation: 3046

You can combine a split() and a ' 'join() to accomplish this:

df1 = pd.DataFrame({'Item_Description': ['dfd_landing BKK_AOT-Airbridge / Jetty', 'dfd_month DEL_Updater Services Pvt Ltd-Office Cleaning']})
df1['Item_Description'] = df1['Item_Description'].apply(lambda x : ' '.join(x.split(' ')[1:]))
df1

Upvotes: 0

Related Questions