2

I'm new to Python and Pandas and I'm trying to replace all null values in an array with a specific value.

Everytime I run this, the updated values don't persist.

I've seen that Pandas doesn't save changes when iterating rows...so how CAN I save the changes?

Here is my code

animal_kinds = set(df.AnimalKind) # this gives categories used below in the "ak" like dog, cat, bird
new_color_dog = 'polka dots'
new_color_cat = 'plaid'
new_color_bird = 'stripes'

for ak in animal_kinds:
    ak_colors = ak['colors']


    ak_with_no_color = animals[(df["Kind"] == ak ) & (df["Color"] == "" ) ] 


    result_count = len(ak_with_no_color)
    if result_count:

        ak_with_no_color.at["Color"] = new_color_ak #sets new color based on kind of animal (ak) 
        print(str(ak) 'color is changed to ' + str(new_color_ak))

1 Answer 1

1

Avoid chained indexing

This kind of operation is known as chained indexing and it is explicitly discouraged in the docs:

df[(df['kind'] == 'dog') & (df['colour'] == '')].at['colour'] = 'black'

Instead, calculate and then use a Boolean mask:

mask = (df['kind'] == 'dog') & (df['colour'] == '')
df.loc[mask, 'colour'] = 'black'

Use a dictionary for a variable number of variables

This kind of operation does not work in Python:

new_colour_dog = 'polka dots'
new_colour+'_dog'  # want 'polka dots', but will not work

Use a dictionary instead:

new_colours = {'dog': 'polka dots', 'cat': 'plaid', 'bird': 'stripes'}

You can then iterate your dictionary's key-value pairs:

for animal, new_colour in new_colours.items():
    mask = (df['kind'] == animal) & (df['colour'] == '')
    df.loc[mask, 'colour'] = new_colour

You don't need to test / special-case instances when mask returns a series of False values.

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.