Planets DataFrame select columns



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

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

df = pd.read_csv(filename)

mass = df['Mass'] # Get Series
print(type(mass))
print(mass)
print()

one_col = df[['Mass']] # Get DataFrame
print(type(one_col))
print(one_col)
print()

two_cols = df[['Planet name', 'Mass']] # Select multiple columns, get DataFrame
print(type(two_cols))
print(two_cols)
print()

<class 'pandas.core.series.Series'>
0       0.055000
1       0.815000
2       1.000000
3       0.107000
4       0.000150
5     318.000000
6      95.000000
7      14.000000
8      17.000000
9       0.002180
10      0.000254
Name: Mass, dtype: float64

<class 'pandas.core.frame.DataFrame'>
          Mass
0     0.055000
1     0.815000
2     1.000000
3     0.107000
4     0.000150
5   318.000000
6    95.000000
7    14.000000
8    17.000000
9     0.002180
10    0.000254

<class 'pandas.core.frame.DataFrame'>
   Planet name        Mass
0      Mercury    0.055000
1        Venus    0.815000
2        Earth    1.000000
3         Mars    0.107000
4        Ceres    0.000150
5      Jupiter  318.000000
6       Saturn   95.000000
7       Uranus   14.000000
8      Neptune   17.000000
9        Pluto    0.002180
10      Charon    0.000254