Planets DataFrame select rows



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

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

df = pd.read_csv(filename)

rows = df[2:5] # using row numbers
print(type(rows))
print(rows)
print()

iloc_rows = df.iloc[2:5] # just like plain []
print(type(iloc_rows))
print(iloc_rows)
print()

picked_rows = df.iloc[[2,5,3]] # using specific row numbers
print(type(picked_rows))
print(picked_rows)
print()
# df[[2,5,3]] would not work so we need iloc

<class 'pandas.core.frame.DataFrame'>
  Planet name  Distance (AU)     Mass
2       Earth           1.00  1.00000
3        Mars           1.50  0.10700
4       Ceres           2.77  0.00015

<class 'pandas.core.frame.DataFrame'>
  Planet name  Distance (AU)     Mass
2       Earth           1.00  1.00000
3        Mars           1.50  0.10700
4       Ceres           2.77  0.00015

<class 'pandas.core.frame.DataFrame'>
  Planet name  Distance (AU)     Mass
2       Earth            1.0    1.000
5     Jupiter            5.2  318.000
3        Mars            1.5    0.107