pandas: how to run a pivot with a multi-index?

前端 未结 3 1455
轻奢々
轻奢々 2020-12-07 13:10

I would like to run a pivot on a pandas DataFrame, with the index being two columns, not one. For example, one field for the year, one for the month, an \'item\

3条回答
  •  余生分开走
    2020-12-07 14:11

    thanks to gmoutso comment you can use this:

    def multiindex_pivot(df, index=None, columns=None, values=None):
        if index is None:
            names = list(df.index.names)
            df = df.reset_index()
        else:
            names = index
        list_index = df[names].values
        tuples_index = [tuple(i) for i in list_index] # hashable
        df = df.assign(tuples_index=tuples_index)
        df = df.pivot(index="tuples_index", columns=columns, values=values)
        tuples_index = df.index  # reduced
        index = pd.MultiIndex.from_tuples(tuples_index, names=names)
        df.index = index
        return df
    

    usage:

    df.pipe(multiindex_pivot, index=['idx_column1', 'idx_column2'], columns='foo', values='bar')
    

    You might want to have a simple flat column structure and have columns to be of their intended type, simply add this:

    (df
       .infer_objects()  # coerce to the intended column type
       .rename_axis(None, axis=1))  # flatten column headers
    

提交回复
热议问题