Lesson 6: Renaming and combining
    1 / 10

    Lesson 6 of 6 · about 20 minutes

    Renaming and combining

    Renaming columns

    Column names from files are often long, inconsistent or unclear. rename() changes them. Give it a dictionary of old name to new name:

    import pandas as pd
    
    df = pd.DataFrame({"Student Name": ["Ava", "Ben"], "pts": [88, 92]})
    df = df.rename(columns={"Student Name": "name", "pts": "points"})
    print(df)

    The output:

      name  points
    0  Ava      88
    1  Ben      92

    Like most pandas methods, rename() returns a new table and leaves the old one alone, which is why the result is assigned back to df.