1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Pandas 'rename'-function not working within jupyter notebook?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página