Home > front end >  Pandas : Remove a part of a string before the first white space
Pandas : Remove a part of a string before the first white space

Time:06-25

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

CodePudding user response:

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(' ')

CodePudding user response:

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

CodePudding user response:

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
  • Related