Skip to content Skip to sidebar Skip to footer

Python: Deleting Rows From Dataframe For Which Value Does Not Exist In Dictionary

I have a csv file(or dataframe) like below : Text Location State A Florida, USA Florida B NY New York C D abc And a dictionary with key value p

Solution 1:

Use extract + replace, last remove rows by dropna:

stat_map = {
        'FL': 'Florida',
        'NY': 'New York',
        'AR': 'Arkansas',
}

#get list from all values from keys and values of dict
L = list(stat_map.keys()) + list(stat_map.values())
print (L)
['NY', 'FL', 'AR', 'New York', 'Florida', 'Arkansas']


df['State1'] = df['Location'].str.extract('(' + '|'.join(L) + ')', expand=False)
                             .replace(stat_map)
df = df.dropna(subset=['State1'])
print (df)
  Text      Location     State    State1
0    A  Florida, USA   Florida   Florida
1    B            NY  New York  New York

Post a Comment for "Python: Deleting Rows From Dataframe For Which Value Does Not Exist In Dictionary"