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)
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.
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With