ccsv
ccsv

Reputation: 8669

Efficiently Iterating over dictionary list values by skipping missing values Python 3

I have a pandas dataframe

import pandas as pd

df=pd.DataFrame({'Location': [ 'NY', 'SF', 'NY', 'NY', 'SF', 'SF', 'TX', 'TX', 'TX', 'DC'],
                 'Class': ['H','L','H','L','L','H', 'H','L','L','M'],
                 'Address': ['12 Silver','10 Fak','12 Silver','1 North','10 Fak','2 Fake', '1 Red','1 Dog','2 Fake','1 White'],
                 'Score':['4','5','3','2','1','5','4','3','2','1',]})

And I want to add 2 tags which I stored in dictionaries. Note that the second dictionary does not include key 'A'

df['Tag1'] =''
df['Tag2'] =''

tagset1 = {'A':['NY|SF'],
          'B':['DC'],
          'C':['TX'],
          }
for key in tagset1:
    df.loc[df.Location.str.contains(tagset1[key][0]) & (df.Tag1 == ''),'Tag1'] = key


tagset2= {'B':['H|M'],
          'C':['L'],
          }
for key in tagset2:
    df.loc[df.Class.str.contains(tagset2[key][0]) & (df.Tag2 == ''),'Tag2'] = key

print (df)

If I want to combine the both dictionaries to make the code more readable and efficient should I fill in the spot for A in newtagset['A'][1] with '' or is there another way to make the iterator ignore or skip position newtagset['A'][1] when iterating over the position in the list?

newtagset = {'A':['NY|SF', '',],
          'B':['DC','H|M',],
          'C':['TX','L',],
          }


for key in newtagset:
    df.loc[df.Location.str.contains(newtagset[key][0]) & (df.Tag1 == ''),'Tag1'] = key

for key in newtagset:
    df.loc[df.Class.str.contains(newtagset[key][1]) & (df.Tag2 == ''),'Tag2'] = key

print (df)

Most solutions I found uses itertools Skip multiple iterations in loop python is this the only way?

Upvotes: 2

Views: 2490

Answers (1)

pacholik
pacholik

Reputation: 8982

There is nothing wrong with simple continue.

for key, value in newtagset.items():    # I found dict.items cleaner
    if not value[1]:
        continue
    df.loc...

A bit of off topic:

& (df.Tag1 == '') is redundant. I would be useful only if you had coincidences in values, but that would lead to unpredictable behavior, since dict is not ordered.

Upvotes: 1

Related Questions