set first column as index pandas code example

Example 1: how to change indeces in pandas dataframe

>>> df.set_index('month')
       year  sale
month
1      2012    55
4      2014    40
7      2013    84
10     2014    31

Example 2: set index in datarame

df = df.set_index('col')

Example 3: set type of column pandas

df.astype(int)

Example 4: add an index column pandas

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_index('month')

Example 5: set column datatype pandas

df = pd.read_csv("weather.tsv", sep="\t",  
                 dtype={'Day': str,'Wind':int64})
df.dtypes

Example 6: change index to dataframe pandas

#cree un indice par defaut sur la base de donnee
df.reset_index()