gpt4 book ai didi

python - pandas - 获取未排序的分层列

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:55 24 4
gpt4 key购买 nike

我有以下数据框:

import numpy as np
import pandas as pd
arrays = [['qux', 'qux', 'baz', 'baz', 'foo', 'foo', 'bar', 'bar'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = zip(*arrays)
index = pd.MultiIndex.from_tuples(tuples)
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
print df

输出如下:

    qux                     baz                     foo                     bar
one two one two one two one two
A 0.504208 1.059471 1.488488 0.807279 0.788239 0.110510 0.882414 0.120483
B 0.178940 0.099793 0.460812 -1.388569 1.264663 -0.050531 -0.839683 0.472138
C 0.356101 -0.172082 0.859077 -0.560092 0.450147 1.200750 -0.433077 0.437339

当我尝试获取 level 0 列时,我得到以下信息:

df.columns.levels[0]

输出:

Index([u'bar', u'baz', u'foo', u'qux'], dtype='object', name=u'first')

列正在排序。有什么方法可以不排序就得到 level 0 列。以下方式:

[u'qux', u'baz', u'foo', u'bar']

请帮忙。

最佳答案

您可以使用 Index.get_level_values + Index.unique :

print (df.columns.get_level_values(0).unique())
Index(['qux', 'baz', 'foo', 'bar'], dtype='object')

替代解决方案 Index.drop_duplicates :

print (df.columns.get_level_values(0).drop_duplicates())
Index(['qux', 'baz', 'foo', 'bar'], dtype='object')

关于python - pandas - 获取未排序的分层列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com