pandas read_csv and keep only certain rows (python)
<p>I am aware of the skiprows that allows you to pass a list with the indices of the rows to skip. However, I have the index of the rows I want to keep.</p>

<p>Say that my cvs file looks like this for millions of rows:</p>

<pre><code>  A B
0 1 2
1 3 4
2 5 6
3 7 8
4 9 0
</code></pre>

<p>The list of indices i would like to load are only 2,3, so</p>

<pre><code>index_list = [2,3]
</code></pre>

<p>The input for the skiprows function would be [0,1,4]. However, I only have available [2,3].</p>

<p>I am trying something like:</p>

<pre><code>pd.read_csv(path, skiprows = ~index_list)
</code></pre>

<p>but no luck.. any suggestions?</p>

<p>thank and I appreciate all the help,</p>

<p>I think you would need to find the number of lines first, like <a href="http://stackoverflow.com/a/1019572/2029132">this</a>.</p>

<pre><code>num_lines = sum(1 for line in open('myfile.txt'))
</code></pre>

<p>Then you would need to delete the indices of <code>index_list</code>:</p>

<pre><code>to_exclude = [i for i in num_lines if i not in index_list]
</code></pre>

<p>and then load your data:</p>

<pre><code>pd.read_csv(path, skiprows = to_exclude)
</code></pre>

python pandas