您需要中有
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]])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)