在Pandas中重命名MultiIndex列

din*_*nya 30 pandas

df = pd.DataFrame([[1,2,3], [10,20,30], [100,200,300]])
df.columns = pd.MultiIndex.from_tuples((("a", "b"), ("a", "c"), ("d", "f")))
df
Run Code Online (Sandbox Code Playgroud)

回报

     a         d
     b    c    f
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

df.columns.levels[1]
Run Code Online (Sandbox Code Playgroud)

回报

Index([u'b', u'c', u'f'], dtype='object')
Run Code Online (Sandbox Code Playgroud)

我想重命名"f""e".据pandas.MultiIndex.rename我说:

df.columns.rename(["b1", "c1", "f1"], level=1)
Run Code Online (Sandbox Code Playgroud)

但它提出了

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-110-b171a2b5706c> in <module>()
----> 1 df.columns.rename(["b1", "c1", "f1"], level=1)

C:\Users\USERNAME\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\indexes\base.pyc in set_names(self, names, level, inplace)
    994         if level is not None and not is_list_like(level) and is_list_like(
    995                 names):
--> 996             raise TypeError("Names must be a string")
    997 
    998         if not is_list_like(names) and level is None and self.nlevels > 1:

TypeError: Names must be a string
Run Code Online (Sandbox Code Playgroud)

我用Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]'pandas 0.19.1

EdC*_*ica 31

用途set_levels:

In [22]:
df.columns.set_levels(['b1','c1','f1'],level=1,inplace=True)
df

Out[22]:
     a         d
    b1   c1   f1
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

rename 设置索引的名称,它不重命名列名称:

In [26]:
df.columns = df.columns.rename("b1", level=1)
df

Out[26]:
      a         d
b1    b    c    f
0     1    2    3
1    10   20   30
2   100  200  300
Run Code Online (Sandbox Code Playgroud)

这就是你得到错误的原因

  • 在`python3`中,它是`df.index.set_levels(['b1','c1','f1'],level=1,inplace=True)` (6认同)

jez*_*ael 26

在pandas中0.21.0+使用参数level=1:

d = dict(zip(df.columns.levels[1], ["b1", "c1", "f1"]))
print (d)
{'c': 'c1', 'b': 'b1', 'f': 'f1'}

df = df.rename(columns=d, level=1)
print (df)
     a         d
    b1   c1   f1
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

  • 现在,这是最好的解决方案。 (5认同)

din*_*nya 10

另一件你不能做的事是df.rename(columns={('d', 'f'): ('e', 'g')}),即使它看起来是正确的。换句话说:.rename()没有达到预期,<...>

——卢卡斯评论

“hacky”方式是这样的(就pandas 1.0.5而言)

def rename_columns(df, columns, inplace=False):
    """Rename dataframe columns.

    Parameters
    ----------
    df : pandas.DataFrame
        Dataframe.
    columns : dict-like
        Alternative to specifying axis. If `df.columns` is
        :obj: `pandas.MultiIndex`-object and has a few levels, pass equal-size tuples.

    Returns
    -------
    pandas.DataFrame or None
        Returns dataframe with modifed columns or ``None`` (depends on `inplace` parameter value).
    
    Examples
    --------
    >>> columns = pd.Index([1, 2, 3])
    >>> df = pd.DataFrame([[1, 2, 3], [10, 20, 30]], columns=columns)
    ...     1   2   3
    ... 0   1   2   3
    ... 1  10  20  30
    >>> rename_columns(df, columns={1 : 10})
    ...    10   2   3
    ... 0   1   2   3
    ... 1  10  20  30
    
    MultiIndex
    
    >>> columns = pd.MultiIndex.from_tuples([("A0", "B0", "C0"), ("A1", "B1", "C1"), ("A2", "B2", "")])
    >>> df = pd.DataFrame([[1, 2, 3], [10, 20, 30]], columns=columns)
    >>> df
    ...    A0  A1  A2
    ...    B0  B1  B2
    ...    C0  C1
    ... 0   1   2   3
    ... 1  10  20  30
    >>> rename_columns(df, columns={("A2", "B2", "") : ("A3", "B3", "")})
    ...    A0  A1  A3
    ...    B0  B1  B3
    ...    C0  C1
    ... 0   1   2   3
    ... 1  10  20  30
    """
    columns_new = []
    for col in df.columns.values:
        if col in columns:
            columns_new.append(columns[col])
        else:
            columns_new.append(col)
    columns_new = pd.Index(columns_new, tupleize_cols=True)

    if inplace:
        df.columns = columns_new
    else:
        df_new = df.copy()
        df_new.columns = columns_new
        return df_new
Run Code Online (Sandbox Code Playgroud)

所以就

>>> df = pd.DataFrame([[1,2,3], [10,20,30], [100,200,300]])
>>> df.columns = pd.MultiIndex.from_tuples((("a", "b"), ("a", "c"), ("d", "f")))
>>> rename_columns(df, columns={('d', 'f'): ('e', 'g')})
...      a         e
...      b    c    g
... 0    1    2    3
... 1   10   20   30
... 2  100  200  300
Run Code Online (Sandbox Code Playgroud)

pandas 团队对此有何看法?为什么这种行为不是默认行为?


gie*_*s0r 9

还有(代码)index.set_names

df.index.set_names(["b1", "c1", "f1"], inplace=True)
Run Code Online (Sandbox Code Playgroud)


Ynj*_*jmh 6

你可以pandas.DataFrame.rename()直接使用

假设您有以下数据框

print(df)

     a         d
     b    c    f
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)
df = df.rename(columns={'f': 'f1', 'd': 'd1'})
Run Code Online (Sandbox Code Playgroud)
print(df)

     a        d1
     b    c   f1
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

你看,列名映射器与级别无关。

假设您有以下数据框

print(df)

     a        d1
     b    c   f1
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

如果你想重命名funder a,你可以这样做

     a         d
     b    f    f
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)
print(df)

     a         d
     b   af    f
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

  • 我认为这个答案最好地解释了这一点。 (2认同)
  • @ThomasHilger 将 MultiIndex 转换为元组列表,因为元组可以在“重命名”中匹配。另一种选择是使用 [pandas.MultiIndex.to_flat_index](https://pandas.pydata.org/docs/reference/api/pandas.MultiIndex.to_flat_index.html)。 (2认同)

Gon*_*ica 5

另一种方法是使用pandas.Series.maplambda 函数,如下所示

df.columns = df.columns.map(lambda x: (x[0], "e") if x[1] == "f" else x)

[Out]:
     a         d
     b    c    e
0    1    2    3
1   10   20   30
2  100  200  300
Run Code Online (Sandbox Code Playgroud)

  • 这个答案被低估了 (2认同)