您的位置: 网站首页> Pandas教程> 当前文章

pandas获取及删除某一级索引、多层变为单层索引

老董-我爱我家房产SEO2022-08-14194围观,139赞

  pandas创建多重索引后,如果想要得到某一层的索引,则需要通过get_level_values获得。如果想要删除某个层级的索引则可以使用

  get_level_values(level=)函数可以接受int或者str类型,int代表索引层级,0代表最外层;str则代表索引的name属性值。鄙人经过测试该函数不能直接获取二维列索引某个层级的值!

# ‐*‐ coding: utf‐8 ‐*‐
import pandas as pd
import numpy as np  


df = pd.DataFrame({"name": ["baidu", "baidu", "baidu", "baidu", "baidu",
                         "google", "google", "google", "google"],
                   "duankou": ["pc", "pc", "pc", "wap", "wap",
                         "pc", "pc", "wap", "wap"],
                   "ispass": ['ok','no', 'ok','no','ok','no','ok','no','ok',],
                   "value": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
table = pd.pivot_table(df, values=['value'], index=['name', 'duankou'],columns = ['ispass'],aggfunc=np.sum)
print(table)
print('--------------')
print(table.index.names)
index1 = table.index.get_level_values(level=0) # 外层
index2 = table.index.get_level_values(level='duankou') # 内层

print(index1)
print(index2)
print(table.columns.names)
# 会报错Level ispass not found
# colindex = table.index.get_level_values(level='ispass') 

               value   
ispass            no ok
name   duankou         
baidu  pc          4  7
       wap         5  6
google pc          6  8
       wap         9  9
--------------
['name', 'duankou']
Index(['baidu', 'baidu', 'google', 'google'], dtype='object', name='name')
Index(['pc', 'wap', 'pc', 'wap'], dtype='object', name='duankou')
[None, 'ispass']

  droplevel(level=0)函数可以删除某个层级的索引,默认level=0代表最外层行索引。level可以接受int, str, 或者list-like,如果事字符串,则代表索引的名称。如果是列表,则就是多个索引的名称;droplevel()函数也可以借助axis参数来删除删除列索引。

# ‐*‐ coding: utf‐8 ‐*‐
# ‐*‐ coding: utf‐8 ‐*‐
import pandas as pd
import numpy as np  


df = pd.DataFrame({"name": ["baidu", "baidu", "baidu", "baidu", "baidu",
                         "google", "google", "google", "google"],
                   "duankou": ["pc", "pc", "pc", "wap", "wap",
                         "pc", "pc", "wap", "wap"],
                   "ispass": ['ok','no', 'ok','no','ok','no','ok','no','ok',],
                   "value": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
table = pd.pivot_table(df, values=['value'], index=['name', 'duankou'],columns = ['ispass'],aggfunc=np.sum)
print(table)
print('--------------')
df1 = table.droplevel(level=0)
print(df1)
print('===============')
df2 = table.droplevel(level='ispass',axis=1) # 删除列索引
print(df2)

               value   
ispass            no ok
name   duankou         
baidu  pc          4  7
       wap         5  6
google pc          6  8
       wap         9  9
--------------
        value   
ispass     no ok
duankou         
pc          4  7
wap         5  6
pc          6  8
wap         9  9
===============
                value  value
name   duankou              
baidu  pc           4      7
       wap          5      6
google pc           6      8
       wap          9      9

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

上一篇:pandas创建多重索引的7种方法

下一篇:没有了

文章评论

    pandas获取及删除某一级索引、多层变为单层索引文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200