Pandas Planets - Add calculated column, remove / delete column (drop)
examples/pandas/add_column_drop_column.py
import pandas as pd df = pd.read_csv('planets.csv') print(df.head()) print() df['dm'] = df['Distance (AU)'] * df['Mass'] print(df.head()) print() df.drop(columns = 'Mass', inplace=True) print(df.head())
Planet name Distance (AU) Mass 0 Mercury 0.40 0.05500 1 Venus 0.70 0.81500 2 Earth 1.00 1.00000 3 Mars 1.50 0.10700 4 Ceres 2.77 0.00015 Planet name Distance (AU) Mass dm 0 Mercury 0.40 0.05500 0.022000 1 Venus 0.70 0.81500 0.570500 2 Earth 1.00 1.00000 1.000000 3 Mars 1.50 0.10700 0.160500 4 Ceres 2.77 0.00015 0.000415 Planet name Distance (AU) dm 0 Mercury 0.40 0.022000 1 Venus 0.70 0.570500 2 Earth 1.00 1.000000 3 Mars 1.50 0.160500 4 Ceres 2.77 0.000415