26
loading...
This website collects cookies to deliver better user experience
pd.DataFrame()
method. pd.MultiIndex.from_tuples()
. This allows you to set mutli index for the columns of the dataframe. import pandas as pd
df = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]])
df.columns = pd.MultiIndex.from_tuples((("a", "b"), ("a", "c"), ("a", "d")))
df
a
b c d
0 1 2 3
1 4 5 6
2 7 8 9
df.columns.set_levels(['b1','c1','d1'],level=1,inplace=True)
df
['b1','c1','d1']
- New column names of the indexlevel=1
- Level of the columns to be renamedinplace=True
- To perform the rename operation in the same dataframe rather than creating the new dataframea
b1 c1 d1
0 1 2 3
1 4 5 6
2 7 8 9
set_levels()
method.