Creating Multi-Index From Tuples |
pd.MultiIndex.from_tuples(tuple) |
index = pd.MultiIndex.from_tuples([('California', 2000), ('California', 2010)]) |
Creating Multi-Index From Arrays |
pd.MultiIndex.from_arrays(list) |
pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'], [1, 2, 1, 2]]) |
Creating Multi-Index From Product |
pd.MultiIndex.from_product([index1_list,index2_list]) |
pd.MultiIndex.from_product([['a', 'b'], [1, 2]]) |
Creating Multi-Index From DataFrame Values |
pd.MultiIndex.from_frame(dataframe) |
df = pd.DataFrame([['a', 'b'], [1, 2]]) pd.MultiIndex.from_frame(df) |
Applying Muli-Index |
data_struct.reindex(index) |
pop = pop.reindex(index) |
Setting Index From Columns |
data_struct.set_index([cols]) |
pop_flat.set_index(['population’]) |
Accessing Multi-Indexed Data Structures |
data_struct[first_index,second_index,....., col] |
pop[:, 2010] # gets all rows from first index and only 2010 rows from second index |
Unstacking |
data_struct.unstack() |
pop.unstack() # this converts the last index (if we have 2 then the second one) values into cols |
Stacking |
data_struct.stack() |
pop.stack() # this converts columns into a second index |
Naming Multi-Indexes |
data_struct.index.names = list |
pop.index.names = ['state', 'year’] |
Swapping Multi-Indexes |
data_struct.swaplevel(0,1) |
pop_df = pop_df.swaplevel(0,1) |
Dropping Multi-Indexes |
data_struct.droplevel(level=index) |
pop_df.droplevel(level=0) |
Multi-Index In Columns |
pd.DataFrame(data, index=multi_index_rows, columns=multi_indx_cols) |
columns = pd.MultiIndex.from_product([['Bob', 'Guido', 'Sue'], ['HR', 'Temp']], names=['subject', 'type']) health_data = pd.DataFrame(data, index=index, columns=columns) |
Slicing Using Multi-Index Column Values |
dataframe[multi_ind_col_value] |
health_data['Guido’] |
Slicing Multi-Index Cols & Rows Using IndexSlice |
idx = pd.IndexSlice df.loc[idx[index_row1,index_row2], idx[index_col1,index_col2]] |
idx = pd.IndexSlice health_data.loc[idx[:, 1], idx[:, 'HR’]] |
Resetting Multi-Index to Cols |
data_struct.reset_index() |
pop.reset_index(name='population’) |
Sorting Multi-Index |
data_struct.sort_index() |
data.sort_index() |