Reference two sequences in for loop, then update first sequence based on their conditions - python

211 views Asked by At

Trying to reference two sequences in a python for loop such as:

for (col1_row, col2_row) in (df.col1, df.col2)

Then check if two conditions are true:

if col1_row = 'nan' and col2_row = '1000 Main Street'

Then update the corresponding cell in df.col1 with a set value:

df.col1 == 'Chucky Cheeze Restaurant'

Does not have to be a for loop, but I thought I'd explain it this way. Looking for fastest, most efficient method. Thanks.

1

There are 1 answers

4
jezrael On BEST ANSWER

I suggest not use loops, because slow, better is use numpy.where with boolean mask:

mask = (df.col1 == 'nan') & (df.col2 == '1000 Main Street')
#for oldier pandas versions
#mask = df.col1.isnull() & (df.col2 == '1000 Main Street')
df.col1 = np.where(mask, 'Chucky Cheeze Restaurant', df.col1)

Or:

mask = df.col2 == '1000 Main Street'
df.loc[mask, 'col1'] = df.loc[mask, 'col1'].fillna('Chucky Cheeze Restaurant')