Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering pandas dataframe in python

I have a csv file with rows and columns separated by commas. This file contains headers (str) and values. Now, I want to filter all the data with a condition. For example, there is a header called "pmra" and I want to keep all the information for pmra values between -2.6 and -2.0. How can I do that? I tried with np.where but it did not work. Thanks for your help.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


filename="NGC188_C.csv"

data = pd.read_csv(filename)

ra       = data["ra"]
dec      = data["dec"]
parallax = data["parallax"]
pm_ra    = data["pmra"]
pm_dec   = data["pmdec"]
g_band   = data["phot_g_mean_mag"]
bp_rp    = data["bp_rp"]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use something like:

data[(data["pmra"] >= -2.6) & (data["pmra"] <= -2)]

There is also another approach: You can use between function:

data["pmra"].between(-2.6, -2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading