栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

熊猫multiIndex完全复制到数据帧切片

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

熊猫multiIndex完全复制到数据帧切片

您需要中有

remove_unused_levels
什么新功能
pandas0.20.0
,还可以检查docs:

new_df.columns.remove_unused_levels()

样品:

np.random.seed(23)cols = pd.MultiIndex.from_tuples([('Iter1','a'), ('Iter1','b'),    ('Iter2','c'), ('Iter2','d'),    ('Iter3','e'), ('Iter3','f')])idx = pd.date_range('2015-01-01', periods=5)df = pd.Dataframe(np.random.rand(5,6), columns=cols, index=idx)print (df)    Iter1    Iter2    Iter3       a         b         c         d         e         f2015-01-01  0.517298  0.946963  0.765460  0.282396  0.221045  0.6862222015-01-02  0.167139  0.392442  0.618052  0.411930  0.002465  0.8840322015-01-03  0.884948  0.300410  0.589582  0.978427  0.845094  0.0650752015-01-04  0.294744  0.287934  0.822466  0.626183  0.110478  0.0005292015-01-05  0.942166  0.141501  0.421597  0.346489  0.869785  0.428602

new_df = df[['Iter1','Iter2']].copy()print (new_df)    Iter1    Iter2       a         b         c         d2015-01-01  0.517298  0.946963  0.765460  0.2823962015-01-02  0.167139  0.392442  0.618052  0.4119302015-01-03  0.884948  0.300410  0.589582  0.9784272015-01-04  0.294744  0.287934  0.822466  0.6261832015-01-05  0.942166  0.141501  0.421597  0.346489print (new_df.columns)MultiIndex(levels=[['Iter1', 'Iter2', 'Iter3'], ['a', 'b', 'c', 'd', 'e', 'f']],labels=[[0, 0, 1, 1], [0, 1, 2, 3]])print (new_df.columns.remove_unused_levels())MultiIndex(levels=[['Iter1', 'Iter2'], ['a', 'b', 'c', 'd']],labels=[[0, 0, 1, 1], [0, 1, 2, 3]])new_df.columns = new_df.columns.remove_unused_levels()print (new_df.columns)MultiIndex(levels=[['Iter1', 'Iter2'], ['a', 'b', 'c', 'd']],labels=[[0, 0, 1, 1], [0, 1, 2, 3]])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661604.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号