Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: How to read specific rows from a CSV file

I have a csv file example.csv like-

    name  |  hits
   ---------------
     A    |  34
     B    |  30
     C    |  25
     D    |  20

Using pandas in Python, how do I only read the rows with hits > 20? Looking for something like-

my_df = pd.read_csv('example.csv', where col('hits') > 20)

like image 912
kev Avatar asked Mar 21 '19 16:03

kev


People also ask

How do I read specific rows in pandas?

In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.


2 Answers

Read the entire csv and do filtering like below

my_df =  pd.read_csv("example.csv")
my_df = my_df[my_df['hits']>20]

If you are having memory issues while reading, you can set chunksize parameter to read it in chunks

like image 115
Sociopath Avatar answered Oct 25 '22 04:10

Sociopath


Read the entire csv and then use query() method to select the required section :

required_df = my_df.query("hits > 20")

or,

required_df =df.loc[df['hits']>20]
like image 36
Loochie Avatar answered Oct 25 '22 02:10

Loochie