DataFrame filter rows by value in list



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

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

df = pd.read_csv(filename)


planet_selector = df['Planet name'].isin(['Earth', 'Mars'])
print(planet_selector)
print()

planets = df[ planet_selector ]
print(planets)

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

  Planet name  Distance (AU)   Mass
2       Earth            1.0  1.000
3        Mars            1.5  0.107