DataFrame filter elementwise boolean and



examples/pandas/planets_filter_elementwise.py
import sys
import pandas as pd

filename = "planets.csv"
if len(sys.argv) == 2:
    filename = sys.argv[1]

df = pd.read_csv(filename)

selector = (df['Mass'] > 1) & (df['Mass'] < 100)
print(selector)
print()

planets = df[ selector ]
print(planets)

0     False
1     False
2     False
3     False
4     False
5     False
6      True
7      True
8      True
9     False
10    False
Name: Mass, dtype: bool

  Planet name  Distance (AU)  Mass
6      Saturn            9.5  95.0
7      Uranus           19.6  14.0
8     Neptune           30.0  17.0