Get the particular rows in Python
<p>I have two csv files. </p>

<p>One is as follows:</p>

<pre><code>"CONS_NO","DATA_DATE","KWH_READING","KWH_READING1","KWH"
"1652714033","2015/1/12","4747.3800","4736.8000","10.5800"
"3332440062","2015/1/12","408.6800","407.8200","0.8600"
"7804314033","2015/1/12","1794.3500","1792.5000","1.8500"
"0114314033","2015/1/12","3525.2000","3519.4400","5.7600"
"1742440062","2015/1/12","3097.1900","3091.4100","5.7800"
"8230100023","2015/1/12","1035.0500","1026.8400","8.2100"
</code></pre>

<p>About six million rows in all.</p>

<p>The other is as follows:</p>

<pre><code>6360609057
8771218657
1338004100
2500009393
9184968250
9710581700
8833903141
</code></pre>

<p>About 10 thousand rows in all.</p>

<p>The second csv file has the CONS_NO only. I want to find the rows in the first csv file corresponding to the number in the second csv file; and delete the other rows in the first csv file in Python.</p>

<p>You can merge the two <code>DataFrame</code> using the merge method in <code>pandas</code>.</p>

<p>I change your example data to the following:</p>

<p><code>test1.csv</code> is:</p>

<pre><code>"CONS_NO","DATA_DATE","KWH_READING","KWH_READING1","KWH"
"1652714033","2015/1/12","4747.3800","4736.8000","10.5800"
"3332440062","2015/1/12","408.6800","407.8200","0.8600"
"7804314033","2015/1/12","1794.3500","1792.5000","1.8500"
"8833903141","2015/1/12","3525.2000","3519.4400","5.7600"
"1742440062","2015/1/12","3097.1900","3091.4100","5.7800"
"8833903141","2015/1/12","1035.0500","1026.8400","8.2100"
</code></pre>

<p>`test2.csv' is:</p>

<pre><code>6360609057
8771218657
1338004100
2500009393
9184968250
9710581700
8833903141
</code></pre>

<p>you can now merge them using the following code:</p>

<pre><code>import pandas as pd

df1 = pd.read_csv('test1.csv')
df2 = pd.read_csv('test2.csv', names=['CONS_NO'])

pd.merge(df1, df2, on='CONS_NO')
</code></pre>

<p>it gives the following output:</p>

<pre><code>    CONS_NO     DATA_DATE   KWH_READING KWH_READING1    KWH
0   8833903141  2015/1/12   3525.20     3519.44         5.76
1   8833903141  2015/1/12   1035.05     1026.84         8.21
</code></pre>

python csv pandas numpy