Ben Chuanlong Du's Blog

It is never too late to learn.

Change the Index of a pandas DataFrame

Assign Index

In [1]:
import pandas as pd

df = pd.DataFrame(
    {"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]}, index=["r1", "r2", "r3", "r4", "r5"]
)

df.head()
Out[1]:
x y
r1 1 5
r2 2 4
r3 3 3
r4 4 2
r5 5 1
In [2]:
df.index = df.y
df
Out[2]:
x y
y
5 1 5
4 2 4
3 3 3
2 4 2
1 5 1

set_index

In [21]:
import pandas as pd

df = pd.DataFrame(
    {"month": [1, 4, 7, 10], "year": [2012, 2014, 2013, 2014], "sale": [55, 40, 84, 31]}
)
df
Out[21]:
month year sale
0 1 2012 55
1 4 2014 40
2 7 2013 84
3 10 2014 31
In [22]:
df2 = df.set_index("month")
df2
Out[22]:
year sale
month
1 2012 55
4 2014 40
7 2013 84
10 2014 31
In [23]:
df2.columns
Out[23]:
Index(['year', 'sale'], dtype='object')
In [7]:
df.year.iat[0]
Out[7]:
2012
In [8]:
df.year.at[0]
Out[8]:
2012
In [9]:
df2.year.iat[0]
Out[9]:
2012
In [10]:
df2.year.at[1]
Out[10]:
2012
In [24]:
df3 = df.set_index(["year", "month"])
df3
Out[24]:
sale
year month
2012 1 55
2014 4 40
2013 7 84
2014 10 31
In [16]:
import numpy as np
In [17]:
np.average([20000, 30000], weights=[9, 15])
Out[17]:
26250.0
In [ ]:
 

Comments