Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Renaming Columns | Data Cleaning
Introduction to Pandas with AI

bookRenaming Columns

AI in Action

import pandas as pd

df = pd.read_csv("passengers.csv")

df = df.rename(columns={"Fare": "TicketPrice"})
df.columns = df.columns.str.lower()

Renaming Specific Columns

When you need to rename one or several columns, use the .rename() method. Just pass a dictionary with old names as keys and the new ones as values:

123456789
import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.columns) # Rename Fare into TicketPrice and Embarked into Port df = df.rename(columns={"Fare": "TicketPrice", "Embarked": "Port"}) print(df.columns)
copy

Renaming All Columns

The .rename() method works well when you only need to change a few column names. But if you want to rename every column, it's easier to assign a full list of new names directly:

12345678910111213
import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.columns) # Rename all columns df.columns = [ "id", "class", "name", "sex", "age", "siblings_spouses", "parents_children", "ticket", "fare", "cabin", "port", "ticket_date" ] print(df.columns)
copy

The list must contain the same number of names as there are columns. If the lengths don't match, pandas will raise an error.

Applying Text Transformations

Pandas allows you to apply string operations to column names to improve consistency and readability:

123456789101112131415161718192021
import pandas as pd df = pd.DataFrame({ "Passenger Name": ["John Smith", "Anna Brown", "Mike Johnson"], "Ticket Number ": ["A/5 21171", "PC 17599", "STON/O2. 3101282"], " Port of Embarkation": ["S", "C", "Q"] }) print(df.columns) # Convert all column names to lowercase df.columns = df.columns.str.lower() print(df.columns) # Remove leading or trailing spaces df.columns = df.columns.str.strip() print(df.columns) # Replace spaces with underscores df.columns = df.columns.str.replace(" ", "_") print(df.columns)
copy

You can also modify column names by adding prefixes or suffixes. This is especially useful when combining multiple datasets, as it helps avoid name conflicts and makes column origins clearer:

123456789101112
import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.columns) # Add a prefix to all column names df = df.add_prefix("passenger_") # Add a suffix to all column names df = df.add_suffix("_info") print(df.columns)
copy
Note
Note

All these operations affect only the column names, not the data stored in the columns.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 5

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookRenaming Columns

Veeg om het menu te tonen

AI in Action

import pandas as pd

df = pd.read_csv("passengers.csv")

df = df.rename(columns={"Fare": "TicketPrice"})
df.columns = df.columns.str.lower()

Renaming Specific Columns

When you need to rename one or several columns, use the .rename() method. Just pass a dictionary with old names as keys and the new ones as values:

123456789
import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.columns) # Rename Fare into TicketPrice and Embarked into Port df = df.rename(columns={"Fare": "TicketPrice", "Embarked": "Port"}) print(df.columns)
copy

Renaming All Columns

The .rename() method works well when you only need to change a few column names. But if you want to rename every column, it's easier to assign a full list of new names directly:

12345678910111213
import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.columns) # Rename all columns df.columns = [ "id", "class", "name", "sex", "age", "siblings_spouses", "parents_children", "ticket", "fare", "cabin", "port", "ticket_date" ] print(df.columns)
copy

The list must contain the same number of names as there are columns. If the lengths don't match, pandas will raise an error.

Applying Text Transformations

Pandas allows you to apply string operations to column names to improve consistency and readability:

123456789101112131415161718192021
import pandas as pd df = pd.DataFrame({ "Passenger Name": ["John Smith", "Anna Brown", "Mike Johnson"], "Ticket Number ": ["A/5 21171", "PC 17599", "STON/O2. 3101282"], " Port of Embarkation": ["S", "C", "Q"] }) print(df.columns) # Convert all column names to lowercase df.columns = df.columns.str.lower() print(df.columns) # Remove leading or trailing spaces df.columns = df.columns.str.strip() print(df.columns) # Replace spaces with underscores df.columns = df.columns.str.replace(" ", "_") print(df.columns)
copy

You can also modify column names by adding prefixes or suffixes. This is especially useful when combining multiple datasets, as it helps avoid name conflicts and makes column origins clearer:

123456789101112
import pandas as pd df = pd.read_csv("https://staging-content-media-cdn.codefinity.com/courses/64641555-cae4-4cd0-8d29-807aeb6bc0c4/datasets/passengers.csv") print(df.columns) # Add a prefix to all column names df = df.add_prefix("passenger_") # Add a suffix to all column names df = df.add_suffix("_info") print(df.columns)
copy
Note
Note

All these operations affect only the column names, not the data stored in the columns.

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 5
some-alt