Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to change the name in the name column to default for only rows that meet a specific condition (name == None and 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?

like image 414
Jane Sully Avatar asked Dec 31 '25 10:12

Jane Sully


1 Answers

a fast, solid, robust Panda-esque solution:

df.loc[(df.name == None) & (df.weight == 1), 'name'] = 'defaut'
like image 134
ℕʘʘḆḽḘ Avatar answered Jan 02 '26 23:01

ℕʘʘḆḽḘ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!