Work with a row in a pandas dataframe without incurring chain indexing (not coping just indexing)
<p>My data is organized in a dataframe:</p>

<pre><code>import pandas as pd
import numpy as np

data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB', 'AAA', 'CCC']}

df = pd.DataFrame(data=data, index = ['R1','R2','R3','R4'])
</code></pre>

<p>Which looks like this (only much bigger):</p>

<pre><code>    Col1  Col2  Col3 Col4
R1     4    10   100  AAA
R2     5    20    50  BBB
R3     6    30   -30  AAA
R4     7    40   -50  CCC
</code></pre>

<p>My algorithm loops through this table rows and performs a set of operations. </p>

<p>For cleaness/lazyness sake, I would like to work on a single row at each iteration without typing <code>df.loc['row index', 'column name']</code> to get each cell value</p>

<p>I have tried to follow the <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy" rel="nofollow">right style</a> using for example:</p>

<pre><code>row_of_interest = df.loc['R2', :]
</code></pre>

<p>However, I still get the warning when I do:</p>

<pre><code>row_of_interest['Col2'] = row_of_interest['Col2'] + 1000

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
</code></pre>

<p>And it is not working (as I intended) it is making a copy</p>

<pre><code>print df

    Col1  Col2  Col3 Col4
R1     4    10   100  AAA
R2     5    20    50  BBB
R3     6    30   -30  AAA
R4     7    40   -50  CCC
</code></pre>

<p>Any advice on the proper way to do it? Or should I just stick to work with the data frame directly?</p>

<p>Edit 1:</p>

<p>Using the replies provided the warning is removed from the code but the original dataframe is not modified: The "row of interest" <code>Series</code> is a copy not part of the original dataframe. For example:</p>

<pre><code>import pandas as pd
import numpy as np

data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB', 'AAA', 'CCC']}

df = pd.DataFrame(data=data, index = ['R1','R2','R3','R4'])

row_of_interest         = df.loc['R2']
row_of_interest.is_copy = False
new_cell_value          = row_of_interest['Col2'] + 1000
row_of_interest['Col2'] = new_cell_value

print row_of_interest 

Col1       5
Col2    1020
Col3      50
Col4     BBB
Name: R2, dtype: object

print df

    Col1  Col2  Col3 Col4
R1     4    10   100  AAA
R2     5    20    50  BBB
R3     6    30   -30  AAA
R4     7    40   -50  CCC
</code></pre>

<p>Edit 2:</p>

<p>This is an example of the functionality I would like to replicate. In python a list of lists looks like:</p>

<pre><code>a = [[1,2,3],[4,5,6]]
</code></pre>

<p>Now I can create a "label" </p>

<pre><code>b = a[0]
</code></pre>

<p>And if I change an entry in b:</p>

<pre><code>b[0] = 7
</code></pre>

<p>Both a and b change.</p>

<pre><code>print a, b

[[7,2,3],[4,5,6]], [7,2,3]
</code></pre>

<p>Can this behavior be replicated between a pandas dataframe labeling one of its rows a pandas series?</p>

<p>You can remove the warning by creating a series with the slice you want to work on:</p>

<pre><code>from pandas import Series
row_of_interest = Series(data=df.loc['R2', :])
row_of_interest.loc['Col2'] += 1000
print(row_of_interest)
</code></pre>

<p>Results in:</p>

<pre><code>Col1       5
Col2    1020
Col3      50
Col4     BBB
Name: R2, dtype: object
</code></pre>

python pandas indexing dataframe series