Python – Pandas : Select CSV records by criteria

      No Comments on Python – Pandas : Select CSV records by criteria

Having CSV file like this loaded as pandas dataframe

I am interested only in records with latitude>10 and longitude<60. To obtain such a dataframe, we can do

Gives us desired data

However maybe we don't want to hardcode names of used columns and want to stay dynamic. In that case, we can specify column name by string

The way this works underhood is actually just plain masking. You can try to run

Gives output of pandas.series type of booleans.

Therefore, this mask is just in the end just used to mask whether n-th record out of all records should be use or not. If you would like to play with this data along, here are the CSV data to load into pandas

Leave a Reply

Your email address will not be published. Required fields are marked *