Pandas: Change values chosen by boolean indexing in a column without getting a warning
<p>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 <code>iloc</code> at the moment and it either does not work or I'm getting that annoying warning:</p>

<blockquote>
  <p>A value is trying to be set on a copy of a slice from a DataFrame</p>
</blockquote>

<p>Example:</p>

<pre><code>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']})
</code></pre>

<p>Doing one of those</p>

<pre><code>DF['B'].iloc[:][DF['A'] == 1] = 'X'

DF.iloc[:]['B'][DF['A'] == 1] = 'Y'
</code></pre>

<p>works, but leads to the warning above.</p>

<p>This one also gives a warning, but does not work:</p>

<pre><code>DF.iloc[:][DF['A'] == 1]['B'] = 'Z'
</code></pre>

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

<p>Can someone clear this up for me?</p>

<p>You are chaining you're selectors, leading to the warning.  Consolidate the selection into one.<br>
Use <code>loc</code> instead</p>

<pre><code>DF.loc[DF['A'] == 1, 'B'] = 'X'
DF
</code></pre>

<p><a href="http://i.stack.imgur.com/HX26a.png" rel="nofollow"><img src="http://i.stack.imgur.com/HX26a.png" alt="enter image description here"></a></p>

python python-2.7 pandas indexing condition