-1

I have DataFrame where: if in column C row has no value than based on value in column D I want to assign value to column C

For example: row 1 in column C is empty and same row in column D has value 'cat', so I want fill empty cell in column C with value 'home'

Below my the ways I've tried but none worked:

for line in df1['C']:
for row in df1['D']:
    if line == None:
        if row == ('cat' or 'dog'):
            df1['C'] == 'house'
        elif row == ('horse' or 'cow' or 'bird'):
            df1['C'] == 'garden'
        break
    break

or

df1.loc[df1['D'] == 'cat' and df1['C'] == ' ', 'C'] = 'house' --> and I would write same line for every singe case

I don't know why my loop is not working, I mean the code itself has no errors but nothing changes in my file after running the loop.

I've tried None, nan or first doing fillna() before the rest of the code so finding empty cells is not an issue in this case

  • It's easier to answer this kind of question if you provide a reproducible example, like this: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – w-m Nov 10 '22 at 17:32

1 Answers1

0

i think you can use np.select():

import numpy as np

condlist=[(df1['C'].isnull()) & (df1['D'].str.contains('cat|dog')),((df1['C'].isnull()) & ((df1['D'].str.contains('horse|cow|bird'))))]
choicelist=['house','garden']
default=df1['C']
df1['C']=np.select(condlist,choicelist,default)

Bushmaster
  • 4,196
  • 3
  • 8
  • 28