1

I have a dataframe like this.

I am trying to remove the string which presents in substring column.

Main                     substring
Sri playnig well cricket cricket
sri went out             NaN
Ram is in                NaN
Ram went to UK,US        UK,US

My expected outupt is,

Main                     substring
Sri playnig well         cricket
sri went out             NaN
Ram is in                NaN
Ram went to              UK,US

I tried df["Main"].str.reduce(df["substring"]) but not working, pls help.

jpp
  • 159,742
  • 34
  • 281
  • 339
Pyd
  • 6,017
  • 18
  • 52
  • 109

2 Answers2

2

This one-liner should do it:

df.loc[df['substring'].notnull(), 'Main'] = df.loc[df['substring'].notnull()].apply(lambda x: x['Main'].replace(x['substring'], ''), axis=1)
zipa
  • 27,316
  • 6
  • 40
  • 58
  • thanks for the solution but this takes more time than jpp's solution – Pyd May 08 '18 at 10:55
  • A trivial improvement is to define `mask = df['substring'].notnull()` in a separate line. But then it would be not be a one-liner. – jpp May 08 '18 at 10:57
1

This is one way using pd.DataFrame.apply. Note that np.nan == np.nan evaluates to False, we can use this trick in our function to determine when to apply removal logic.

import pandas as pd, numpy as np

df = pd.DataFrame({'Main': ['Sri playnig well cricket', 'sri went out',
                            'Ram is in' ,'Ram went to UK,US'],
                   'substring': ['cricket', np.nan, np.nan, 'UK,US']})

def remover(row):
    sub = row['substring']
    if sub != sub:
        return row['Main']
    else:
        lst = row['Main'].split()
        return ' '.join([i for i in lst if i!=sub])

df['Main'] = df.apply(remover, axis=1)

print(df)

               Main substring
0  Sri playnig well   cricket
1      sri went out       NaN
2         Ram is in       NaN
3       Ram went to     UK,US
jpp
  • 159,742
  • 34
  • 281
  • 339
  • can you check this one https://datascience.stackexchange.com/questions/31538/how-to-perform-mean-median-operation-on-a-factorial – Pyd May 11 '18 at 17:22