Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Change values chosen by boolean indexing in a column without getting a warning

I have a dataframe, I want to change only those values of a column where another column fulfills a certain condition. I'm trying to do this with iloc at the moment and it either does not work or I'm getting that annoying warning:

A value is trying to be set on a copy of a slice from a DataFrame

Example:

import pandas as pd
DF = pd.DataFrame({'A':[1,1,2,1,2,2,1,2,1],'B':['a','a','b','c','x','t','i','x','b']})

Doing one of those

DF['B'].iloc[:][DF['A'] == 1] = 'X'

DF.iloc[:]['B'][DF['A'] == 1] = 'Y'

works, but leads to the warning above.

This one also gives a warning, but does not work:

DF.iloc[:][DF['A'] == 1]['B'] = 'Z'

I'm really confused about how to do boolean indexing using loc, iloc, and ix right, that is, how to provide row index, column index, AND boolean index in the right order and with the correct syntax.

Can someone clear this up for me?

like image 339
Khris Avatar asked Sep 21 '16 09:09

Khris


1 Answers

You are chaining you're selectors, leading to the warning. Consolidate the selection into one.
Use loc instead

DF.loc[DF['A'] == 1, 'B'] = 'X'
DF

enter image description here

like image 112
piRSquared Avatar answered Sep 19 '22 22:09

piRSquared