Python 使用多索引获取数据帧中未排序的列级别
背景:Python 使用多索引获取数据帧中未排序的列级别,python,pandas,dataframe,Python,Pandas,Dataframe,背景: worms=['worm1', 'worm2', 'worm3'] bodyparts=['head', 'vulva', 'tail'] coords=['x', 'y'] arrays=pd.MultiIndex.from_product([worms, bodyparts, coords], names=['worms', 'bodyparts', 'coords']) idx = pd.Index(np.arange(0
worms=['worm1', 'worm2', 'worm3']
bodyparts=['head', 'vulva', 'tail']
coords=['x', 'y']
arrays=pd.MultiIndex.from_product([worms, bodyparts, coords],
names=['worms', 'bodyparts', 'coords'])
idx = pd.Index(np.arange(0,5000),name='frames')
df=pd.DataFrame(index=idx, columns=arrays)
Index(['head', 'vulva', 'tail'], dtype='object', name='bodyparts')
我有一个多索引的熊猫数据帧
我想获取未排序的columns.levels
df.column.levels将它们提供给我,但已排序
示例:
worms=['worm1', 'worm2', 'worm3']
bodyparts=['head', 'vulva', 'tail']
coords=['x', 'y']
arrays=pd.MultiIndex.from_product([worms, bodyparts, coords],
names=['worms', 'bodyparts', 'coords'])
idx = pd.Index(np.arange(0,5000),name='frames')
df=pd.DataFrame(index=idx, columns=arrays)
Index(['head', 'vulva', 'tail'], dtype='object', name='bodyparts')
看起来像这样:
worms worm1 worm2 worm3
bodyparts head vulva tail head vulva tail head vulva tail
coords x y x y x y x y x y x y x y x y x y
frames
0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
现在如果我这样做:
df.columns.levels[1]
我得到:
Index(['head', 'tail', 'vulva'], dtype='object', name='bodyparts')
我想要什么:
worms=['worm1', 'worm2', 'worm3']
bodyparts=['head', 'vulva', 'tail']
coords=['x', 'y']
arrays=pd.MultiIndex.from_product([worms, bodyparts, coords],
names=['worms', 'bodyparts', 'coords'])
idx = pd.Index(np.arange(0,5000),name='frames')
df=pd.DataFrame(index=idx, columns=arrays)
Index(['head', 'vulva', 'tail'], dtype='object', name='bodyparts')
有任何选项吗?这将按外观顺序提供选项:
pd.unique(df.columns.to_frame()['bodyparts'])
输出: