0

I have am column full of state names. I know how to iterate down through it, but I don't know what syntax to use to have it check for empty values as it goes. tried "isnull()" but that seems to be the wrong approach. Anyone know a way?

was thinking something like:

for state_name in datFrame.state_name:
    if datFrame.state_name.isnull():
        print ('no name value' + other values from row)
    else:
        print(row is good.)

df.head():
 state_name state_ab            city zip_code
0    Alabama       AL       Chickasaw    36611
1    Alabama       AL      Louisville    36048
2    Alabama       AL      Columbiana    35051
3    Alabama       AL         Satsuma    36572
4    Alabama       AL  Dauphin Island    36528


to_dict():
{'state_name': {0: 'Alabama',
  1: 'Alabama',
  2: 'Alabama',
  3: 'Alabama',
  4: 'Alabama'},
 'state_ab': {0: 'AL', 1: 'AL', 2: 'AL', 3: 'AL', 4: 'AL'},
 'city': {0: 'Chickasaw',
  1: 'Louisville',
  2: 'Columbiana',
  3: 'Satsuma',
  4: 'Dauphin Island'},
 'zip_code': {0: '36611', 1: '36048', 2: '35051', 3: '36572', 4: '36528'}}
0

1 Answer 1

1

Based on your description, you can use np.where to check if rows are either null or empty strings.

df['status'] = np.where(df['state'].eq('') | df['state'].isnull(), 'Not Good', 'Good')

(MCVE) For example, suppose you have the following dataframe

    state
0   New York
1   Nevada
2   
3   None
4   New Jersey

then,

    state       status
0   New York    Good
1   Nevada      Good
2               Not Good
3   None        Not Good
4   New Jersey  Good

It's always worth mentioning that you should avoid loops whenever possible, because they are way slower than masking

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

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.