Search code examples
pythonpandasdataframejupyter-notebook

Pandas 'rename'-function not working within jupyter notebook?


I have a pandas dataframe ('df3') which columns I would like to rename.It is a subset of another dataframe, and I'm working in a jupyter notebook.

Getting all infos from the dataframe structure:

df3.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 29 entries, 104 to 132
Data columns (total 15 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   Zählung in   28 non-null     object
 1   Unnamed: 17  29 non-null     object
 2   Unnamed: 18  29 non-null     object
 3   Unnamed: 19  29 non-null     object
 4   Unnamed: 20  29 non-null     object
 5   Unnamed: 21  29 non-null     object
 6   Unnamed: 40  29 non-null     object
 7   Unnamed: 41  29 non-null     object
 8   Unnamed: 42  29 non-null     object
 9   Unnamed: 43  28 non-null     object
 10  Unnamed: 44  28 non-null     object
 11  Unnamed: 63  29 non-null     object
 12  Unnamed: 64  29 non-null     object
 13  Unnamed: 65  29 non-null     object
 14  Unnamed: 66  28 non-null     object
dtypes: object(15)
memory usage: 3.5+ KB

The columns need to be renamed by their index, so I create a dict with the index as keys and the new names as values:

col_names= {0:'Zeit', 
            1:'ZU_PKW', 
            2: 'ZU_LKW1', 
            3: 'ZU_LKW2', 
            4: 'ZU_SV', 
            5: 'ZU_SV%', 
            6:'AB_PKW', 
            7: 'AB_LKW1', 
            8: 'AB_LKW2', 
            9: 'AB_SV', 
            10: 'AB_SV%',
            11:'ALL_PKW', 
            12: 'ALL_LKW1', 
            13: 'ALL_LKW2', 
            14: 'ALL_SV'
           }

I tried to rename the columns with all options using the 'rename'-function:

1. Create a new dataframe with renamed columns without using inplace=True

df4 = df3.rename(index=col_names)

Result: no error, but the columns are not renamed

2. Create a new dataframe with renamed columns by using inplace=True

df4 = df3.rename(index={col_names, inplace = True)

Result: Error: A value is trying to be set on a copy of a slice from a DataFrame And the resulting df is None.

3. Change the dataframe directly by using inplace=True

df3.rename(index={col_names, inplace = True)

Result: `Error: A value is trying to be set on a copy of a slice from a DataFrame and the df3 stays unchanged.

4. Specifying also axis=1

df3.rename(index={col_names, inplace = True)

Returns TypeError: Cannot specify both 'axis' and any of 'index' or 'columns'

I feel I'm running out of options. What am I doing wrong? Could it have something to do with working in the jupyter notebook environment?


Solution

  • If need rename by index of columns set values in list comprehension:

    df.columns = [col_names.get(i, i) for i in range(len(df.columns))]
    

    Or:

    df.columns = pd.Series(range(len(df.columns))).replace(col_names)
    

    If need set first N values by values of dictionary:

    df.columns = list(col_names.values())[:len(df.columns)]
    

    But if really need rename need change columns names to RangeIndex first:

    df = df.set_axis(range(len(df.columns)), axis=1).rename(columns=col_names)