Hello All!
I have a question about boolean indexing and not sure how to proceed.
I am trying to update a column’s value if another column meets a number of different values. In my head, it would be a list object, but I am getting an error.
I will write it out the best I can and hopefully someone can point me in the right direction.
I am trying to update the “afbbillablecourse” field of a data set to be “NotBillable” if the course_no in a different field is a series of course numbers.
Here’s what I have:
course_list=[‘800’,‘801’,‘802’,‘811’,‘866’,‘877’,‘878’,‘899’]
AFBRevenue.loc[AFBRevenue[‘course_no’]=course_list,‘afbbillablecourse’]=‘NotBillable’
Thoughts? Advice?
Thanks!
I think I figured it out, but I am looking for a professional to confirm.
course_list=[‘800’,‘801’,‘802’,‘811’,‘866’,‘877’,‘878’,‘899’]
AFBRevenue.loc[AFBRevenue[‘course_no’].isin(course_list),‘afbbillablecourse’]=‘NotBillable’
It worked and it didn’t throw an error. Is this the best way to do this?
Thanks!
Hi there!
Not a professional by any means, but your solution in your second post should solve your problem; .isin()
will return a Boolean result. This line:
AFBRevenue.loc[AFBRevenue[‘course_no’]=course_list,‘afbbillablecourse’]=‘NotBillable’
doesn’t use any comparison operators.
I think that .isin()
is a good choice here, though I’m still learning too!
Also, just a tip for future reference, it’s easier for someone else to review your code if you surround it with backticks (`), or highlight it and select the preformatted text button at the top of the text box (</>).
Cheers!