Pandas Dataframe row number become the same after selected by condition
<p>So I have 100,000 row of data look like this</p>

<p>df</p>

<pre><code>        date ticker holding 
0   2016-09-22  1   788315240   
1   2016-09-22  2   429232858   
2   2016-09-22  3   1677428346  
3   2016-09-22  4   321595332   
</code></pre>

<p>but when I do:</p>

<pre><code>df.loc[df.ticker == 3]
</code></pre>

<p>it returns</p>

<pre><code>    date    ticker  stockholding    
2   2016-09-22  3   1677428346  
2   2016-09-21  3   1679285716  
2   2016-09-20  3   1680425466  
2   2016-09-19  3   1678823216  
2   2016-09-18  3   1678743180  
2   2016-09-14  3   1682832643  
</code></pre>

<p>you may notice, all row number become 2, even if i do</p>

<pre><code>for i, row in df[df.ticker == 3].iterrows():

  print i
</code></pre>

<p>it print all '2's.</p>

<p>What am I doing wrong here?</p>

<p>You have duplicates values in <code>index</code> only.</p>

<p>You can <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html" rel="nofollow"><code>reset_index</code></a>:</p>

<pre><code>df.reset_index(drop=True, inplace=True)
</code></pre>

<p>Sample:</p>

<pre><code>df = pd.DataFrame({'A':[1,2,3,4,5,8],
                   'B':[4,5,6,0,1,7],
                   'ticker':[7,8,9,3,3,3]},
                   index=[0,1,2,2,2,2])

print (df)
   A  B  ticker
0  1  4       7
1  2  5       8
2  3  6       9
2  4  0       3
2  5  1       3
2  8  7       3

print (df.loc[df.ticker == 3])
   A  B  ticker
2  4  0       3
2  5  1       3
2  8  7       3

df.reset_index(drop=True, inplace=True)

print (df.loc[df.ticker == 3])
   A  B  ticker
3  4  0       3
4  5  1       3
5  8  7       3
</code></pre>

python pandas indexing dataframe condition