其他分享
首页 > 其他分享> > DataFrame.droplevel

DataFrame.droplevel

作者:互联网

函数定义

DataFrame.droplevel(level, axis=0)
Return Series/DataFrame with requested index / column level(s) removed.

函数参数

例子

df = pd.DataFrame([
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
]).set_index([0, 1]).rename_axis(['a', 'b'])
df.columns = pd.MultiIndex.from_tuples([
    ('c', 'e'), ('d', 'f')
], names=['level_1', 'level_2'])
df
level_1   c   d
level_2   e   f
a b
1 2      3   4
5 6      7   8
9 10    11  12
df.droplevel('a')
level_1   c   d
level_2   e   f
b
2        3   4
6        7   8
10      11  12
df.droplevel('level_2', axis=1)
level_1   c   d
a b
1 2      3   4
5 6      7   8
9 10    11  12

标签:11,index,level,df,DataFrame,droplevel,axis
来源: https://www.cnblogs.com/zjuhaohaoxuexi/p/15656784.html