1

I stole this from here Remove/replace columns values based on another columns using pandas

[a.replace(b,'') for a,b in zip(df1['asker'], df1['party']) if a != None]

I added if a != None because it always threw error: AttributeError: 'NoneType' object has no attribute 'replace'

Here are different solutions to the same problem: replacing substring df1['party'] within col asker

df1['new_column'] = df1['asker'].replace(to_replace=r'\b'+df1['party']+r'\b', value='',regex=True)

df1['asker'] = df1.apply(lambda x: x['asker'].replace(x['party'], ''), axis = 1)

None of them worked as soon as i added the exception for None values

example of df1 columns party

[QQQ,
None,
RRR-Fraktion]

example of df1 columns asker

[Konrad Munch QQQ,
None,
Heiko Baer RRR-Fraktion]
1
  • Some problem with solution? Commented Oct 25, 2021 at 12:56

1 Answer 1

1

Use:

[a.replace(b,'') if (a != None) and (b != None) 
                 else a
                 for a,b in zip(df1['asker'], df1['party'])]

If need test NaNs or Nones use notna:

df1 = pd.DataFrame({"asker": ["Heiko Baer RRR-Fraktion", "a", "b", 
                               np.nan, None, None, np.nan], 
                    "party": ['RRR-Fraktion', None, np.nan, 'a', 's', None, np.nan]})
    
df1['asker'] = [a.replace(b,'') if pd.notna(a) and pd.notna(b) 
                                else a 
                                for a,b in zip(df1['asker'], df1['party'])]
print (df1)
         asker         party
0  Heiko Baer   RRR-Fraktion
1            a          None
2            b           NaN
3          NaN             a
4         None             s
5         None          None
6          NaN           NaN
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the suggestion. I had something like this in mind. It doesent remove the substring of party from the asker thouhg. I does nothing. I just tested your second suggestion. It doesent do anything at all, unfortunately. I cannot explain why
@id345678 - Can you be more specific?
I dont know how to be more specific. I could give you an example maybe?
@id345678 - added to sample data in answer. Konrad Munch QQQ is not substring in Fraktion der QQQ so no replacement, so not added to sample data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.