Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Assign Index¶
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()Loading...
df.index = df.y
dfLoading...
set_index¶
import pandas as pd
df = pd.DataFrame(
{"month": [1, 4, 7, 10], "year": [2012, 2014, 2013, 2014], "sale": [55, 40, 84, 31]}
)
dfLoading...
df2 = df.set_index("month")
df2Loading...
df2.columnsIndex(['year', 'sale'], dtype='object')df.year.iat[0]2012df.year.at[0]2012df2.year.iat[0]2012df2.year.at[1]2012df3 = df.set_index(["year", "month"])
df3Loading...
import numpy as npnp.average([20000, 30000], weights=[9, 15])26250.0