Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove unwanted parts from strings in Dataframe

I am looking for an efficient way to remove unwanted parts from strings in a DataFrame column.

My dataframe:

     Passengers

1    Sally Muller, President, Mark Smith, Vicepresident, John Doe, Chief of Staff
2    Sally Muller, President, Mark Smith, Vicepresident
3    Sally Muller, President, Mark Smith, Vicepresident, John Doe, Chief of Staff
4    Mark Smith, Vicepresident, John Doe, Chief of Staff, Peter Parker, Special Effects
5    Sally Muller, President, John Doe, Chief of Staff, Peter Parker, Special Effects, Lydia Johnson, Vice Chief of Staff
...

desired form of df:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     Passengers

1    Sally Muller, Mark Smith, John Doe
2    Sally Muller, Mark Smith
3    Sally Muller, Mark Smith, John Doe
4    Mark Smith, John Doe, Peter Parker
5    Sally Muller, John Doe, Peter Parker, Lydia Johnson
...

Up to now I did it with endless handmade copy/paste regex list:

df = df.replace(r'President,','', regex=True) 
df = df.replace(r'Vicepresident,','', regex=True) 
df = df.replace(r'Chief of Staff,','', regex=True) 
df = df.replace(r'Special Effects,','', regex=True) 
df = df.replace(r'Vice Chief of Staff,','', regex=True) 
...

Is there a more comfortable way to do this?

>Solution :

This is one case where apply is actually faster that explode:

df2 = df['Passengers'].apply(lambda x: ', '.join(x.split(', ')[::2])) #.to_frame() # if dataframe needed

output:

                                          Passengers
0                 Sally Muller, Mark Smith, John Doe
1                           Sally Muller, Mark Smith
2                 Sally Muller, Mark Smith, John Doe
3                 Mark Smith, John Doe, Peter Parker
4  Sally Muller, John Doe, Peter Parker, Lydia Jo...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading