Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding column value for a list of indexes

I have a list of indexes and trying to populate a column 'Type' for these rows only.

What I tried to do:

index_list={1,5,9,10,13} 
df.loc[index_list,'Type']=="gain+loss"

Output:
1      False
5      False
9      False
10     False
13     False

But the output just gives the list with all False instead of populating these rows. Thanks for any advice.

like image 454
Ran Antes Avatar asked Dec 04 '25 09:12

Ran Antes


1 Answers

You need to put a single equal instead of double equal. In python, and in most progamming languages, == is the comparison operator. In your case you need the assignment operator =. So the following code will do what you want :

index_list={1,5,9,10,13} 
df.loc[index_list,'Type']="gain+loss"
like image 88
Basbeu Avatar answered Dec 06 '25 22:12

Basbeu