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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With