0

I need to loop my df.columns so that i can dynamically apply condition to my every column through loop, i am not able to loop my column names in condition.

df:

   B1    B2    B3   B4   B5
   9     0     5    6    7
   8     7     6    4    8
   0     9     8    6    6
   1     0     7    6    3

condition = [(df['B1'] == 0)| (df['B1'].isnull==False),
             (df['B1'] == 8)| (df['B1'].isnull==True),
             (df['B1'] == 6)| (df['B1'].isnull==False)]

values = [999,444,555]

I used to do this:

df['B1'] = np.select(condition , values) # Seperately for every column.

I am trying:

for i in df.columns:
    df[i] = np.select(condition, values) # how can i able to loop i in condition, since condition is constant

Output for B1:

   B1    B2    B3   B4   B5
   999   0     5    6    7 
   999   7     6    4    8
   999   9     8    6    6
   999   0     7    6    3
3
  • 2
    Could you please describe you desired output? How ahould the result look like? Commented Sep 22, 2021 at 14:41
  • @mosc9575 i have added the output for one column "B1" Commented Sep 22, 2021 at 15:03
  • 1
    Every single value has to go through condition, which ever condition satisfies, it will change value. # of condition should be equal to # of values always. Commented Sep 22, 2021 at 15:30

2 Answers 2

1

You can pass a dictionary to .eq() to check equality of different columns with different values:

>>> df.eq({'B1': 0, 'B2': 7, 'B3': 6, 'B4': 4, 'B5': 7})
      B1     B2     B3     B4     B5
0  False  False  False  False   True
1  False   True   True   True  False
2   True  False  False  False  False
3  False  False  False  False  False

Similarly you can then do that after .isna():

>>> df.isna().eq({'B1': False, 'B2': False, 'B3': True, 'B4': False, 'B5': False})
     B1    B2     B3    B4    B5
0  True  True  False  True  True
1  True  True  False  True  True
2  True  True  False  True  True
3  True  True  False  True  True 

And then finally combine both with |

Sign up to request clarification or add additional context in comments.

Comments

0

One possible way is to use mask(), this replaces a value, where a condition is True. If you loop over your conditions and values, I think you get, what you need.

conditions = [(df == 0)| (df.isnull==False),
             (df == 8)| (df.isnull==True),
             (df == 6)| (df.isnull==False)]

values = [999,444,555]        
    
for condition, value in zip(conditions, values):
    df.mask(condition, value, inplace=True)

The output for your example is this

    B1   B2   B3   B4   B5
0    9  999    5  555    7
1  444    7  555    4  444
2  999    9  444  555  555
3    1  999    7  555    3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.