Jane Sully Jane Sully - 1 month ago 8
Python Question

How to change value for rows that meet specific conditions in pandas?

I want to change the name in the name column to

for only rows that meet a specific condition (
name == None
weight == 1
). As you can see weight can be 10, but I do not want to change the names for weights of 10. Here is a small example dataframe:

post name weight response
blah1 None 1 yes
blah2 None 10 no
blah3 Annie 1 yes

How can I achieve this in a simple way?

Answer Source

a fast, solid, robust Panda-esque solution:

df.loc[(df.name == None) & (df.weight == 1), 'name'] = 'defaut'