Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多索引获取数据帧中未排序的列级别_Python_Pandas_Dataframe - Fatal编程技术网

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'])
输出: