Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename unnamed multiindex columns in Pandas DataFrame

Tags:

python

pandas

I created this dataframe:

import pandas as pd
columns = pd.MultiIndex.from_tuples([("x", "", ""), ("values", "a", "a.b"), ("values", "c", "")])
df0 = pd.DataFrame([(0,10,20),(1,100,200)], columns=columns)
df0

I unload df0 to excel:

df0.to_excel("test.xlsx")

and load it again:

df1 = pd.read_excel("test.xlsx", header=[0,1,2])
df1

And I have Unnamed :... column names.

To make df1 look like inital df0 I run:

def rename_unnamed(df, label=""):
    for i, columns in enumerate(df.columns.levels):
        columns = columns.tolist()
        for j, row in enumerate(columns):
            if "Unnamed: " in row:
                columns[j] = ""
        df.columns.set_levels(columns, level=i, inplace=True)
    return df

rename_unnamed(df1)

Well done. But is there any pandas way from box to do this?

like image 344
dinya Avatar asked Nov 28 '16 08:11

dinya


People also ask

How do I rename an unnamed column in a data frame?

You can rename the column in Pandas dataframe using the df. rename( columns={“Old Column Name”:”New Column Name” } ,inplace=True) statement.

How do I rename a MultiIndex in pandas?

To rename the multi index columns of the pandas dataframe, you need to use the set_levels() method. Use the below snippet to rename the multi level columns. where, ['b1','c1','d1'] - New column names of the index.

How do I get rid of unnamed columns in pandas?

Method 1: Use the index = False argument But you should also include index = False argument. It will automatically drop the unnamed column in pandas. And if you want to set the index for the dataframe then you can call the df. set_index() method on any column.


1 Answers

Since pandas 0.21.0 the code should be like this

def rename_unnamed(df):
    """Rename unamed columns name for Pandas DataFrame

    See https://stackoverflow.com/questions/41221079/rename-multiindex-columns-in-pandas

    Parameters
    ----------
    df : pd.DataFrame object
        Input dataframe

    Returns
    -------
    pd.DataFrame
        Output dataframe

    """
    for i, columns in enumerate(df.columns.levels):
        columns_new = columns.tolist()
        for j, row in enumerate(columns_new):
            if "Unnamed: " in row:
                columns_new[j] = ""
        if pd.__version__ < "0.21.0":  # https://stackoverflow.com/a/48186976/716469
            df.columns.set_levels(columns_new, level=i, inplace=True)
        else:
            df = df.rename(columns=dict(zip(columns.tolist(), columns_new)),
                           level=i)
    return df
like image 172
dinya Avatar answered Sep 19 '22 10:09

dinya