Examples
import numpy as np
import pandas as pd
df = pd.DataFrame({'month': [2, 5, 8, 10],
'year': [2017, 2019, 2018, 2019],
'sale': [60, 45, 90, 36]})
df
Set the index to become the ‘month’ column:
df.set_index('month')
Create a MultiIndex using columns ‘year’ and ‘month’:
df.set_index(['year', 'month'])
Create a MultiIndex using an Index and a column:
df.set_index([pd.Index([2, 3, 4, 5]), 'year'])
Create a MultiIndex using two Series:
s = pd.Series([2, 3, 4, 5])
df.set_index([s, s**2])