tonysepia
tonysepia

Reputation: 3500

Conditionally replace values in pandas dataframe - numpy where behaviour

My goal is to replace certain values in a pandas dataframe, based on a condition.

I have discovered that the numpy where method expects the first argument to be an array of boolean values, and the following works:

def evaluate_array(array_to_evaluate):
    output_array = []
    for elem in array_to_evaluate:
        if elem is None:
            output_array.append(True)
        else:
            output_array.append(False)
    return output_array


df['property'] = np.where(
    evaluate_array(df['property']), 
    'The value is None', 
    df.property
)

Question:

There must be a better way to express the lines of code above! I have tried the following:

df.loc[df.property is None, 'property'] = 'None'

but I get the error:

KeyError: 'cannot use a single bool to index into setitem'

the accepted solution to which is as long winded as my own working code!

EDIT: A great solution offered in the comments below works for my situation as follows:

df = df.fillna(value={'property':'The value is None'})

or even more readable:

df['property'].fillna('The value is None', inplace=True)

EDIT2: Also, please see the accepted answer below for another great solution

Upvotes: 1

Views: 411

Answers (1)

Danail Petrov
Danail Petrov

Reputation: 1875

How about

>>> df['property'] = df['property'].fillna('The value is None')

Or alternatively, if None is str you can simply do replace:

>>> df['property'] = df['property'].replace('None','The value is None')

Upvotes: 2

Related Questions