如何将Pandas列multiindex FULL名称作为列表

时间:2018-02-26 02:25:33

标签: python pandas

我有一个pandas DataFrame df

    A          B
    V1   V2    V1   V3    V4
ID 
 1  10   20    30   40    50
 2  60   70    80   90    100

如何获取列表:

>> ["A.V1", "A.V2", "B.V1", "B.V3", "B.V4"] 

Stack Overflow Question提出了类似的问题。但是,提问者只想要'短'名称。如果应用于我的问题,则会导致:

>> ["A", "B"] 

我尝试编写自己的实现:

def get_multiindex_full_names(multiindex):
    if (type(multiindex) != pd.core.index.MultiIndex):
        print("ArgError: Input type is not a MultiIndex!")
        return 

    _lvs = multiindex.levels
    _n = len(_lvs)

    assert(_n > 0)

    _l = _lvs[0]

    def str_cross_product(la, lb):
        """ Returns String Cross product between list a and list b

        @return list of strings
        """
        return [".".join([a, b]) for a in la for b in lb]


    for i in range (1, _n):
        _l = str_cross_product(_l, _lvs[i])

    return _l

然而,这不起作用,因为它会导致:

get_multiindex_full_names(df.columns)
>> ["A.V1", "A.V2", "A.V3", "A.V4", "B.V1", "B.V2", "B.V3", "B.V4"] 

1 个答案:

答案 0 :(得分:3)

尝试:

df.columns.map('.'.join)

测试代码:

df = pd.read_fwf(StringIO(u"""
     ID A    A     B    B     B
     ID V1   V2    V1   V3    V4
     1  10   20    30   40    50
     2  60   70    80   90    100"""),
                 header=[1, 2], index_col=0)
print(df)
print(list(df.columns.map('.'.join)))

结果:

ID   A       B         
ID  V1  V2  V1  V3   V4
1   10  20  30  40   50
2   60  70  80  90  100

['A.V1', 'A.V2', 'B.V1', 'B.V3', 'B.V4']