Home > Enterprise >  Filtering pandas dataframe in python
Filtering pandas dataframe in python

Time:04-10

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"]

CodePudding user response:

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)
  • Related