Pandas:drop row from a dataframe based on a list disregarding the capital first letter

I need to remove from my dataframe the rows that in a column matches the string from a list.

I used wo_list = df[~df.Match.isin(mylist)]

The problem is that some strings have a capital first letter in the list but not in my df.

Thanks for any help.

Answer

One idea is convert both – column and list to lowercase:

mylist = ['Yui','Adf']
df = pd.DataFrame({'Match':['adf','ert','yui']})

wo_list = df[~df.Match.str.lower().isin([str(x).lower() for x in mylist])]
print (wo_list)
  Match
1   ert

Or you can use Series.str.capitalize for column for match list:

mylist = ['Yui','Adf']
df = pd.DataFrame({'Match':['adf','ert','yui']})

wo_list = df[~df.Match.str.capitalize().isin(mylist)]
print (wo_list)
  Match
1   ert

Leave a Reply

Your email address will not be published. Required fields are marked *