Skip to content
Advertisement

Remove a substring from a list of string using Pandas

I’m trying to search some post related to remove substring key but unfortunately those solutions don’t work in my case.

Could you help me ?

Input:

['Female/Fri', 'Male/Fri', 'Female/Sat', 'Male/Sat', 'Female/Sun','Male/Sun', 'Female/Thur', 'Male/Thur', 'Female', 'Male']

Output

['Fri', 'Fri', 'Sat', 'Sat', 'Sun','Sun', 'Thur', 'Thur', 'Female', 'Male']

Advertisement

Answer

You don’t even need pandas for that. A simple list comprehension should be enough

In [3]: [s.split('/')[-1] for s in ['Female/Fri', 'Male/Fri', 'Female/Sat', 'Male/Sat', 'Female/Sun','Male/Sun', 'Female/Thur', 'Male/Thur', 'Female', 'Male']]
Out[3]: ['Fri', 'Fri', 'Sat', 'Sat', 'Sun', 'Sun', 'Thur', 'Thur', 'Female', 'Male']

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement