Renaming 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:
123456789import 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)
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:
12345678910111213import 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)
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:
123456789101112131415161718192021import 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)
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:
123456789101112import 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)
All these operations affect only the column names, not the data stored in the columns.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain how the .rename() method works in more detail?
What other string operations can I apply to column names in pandas?
How do I handle errors if the number of new column names doesn't match the original?
Génial!
Completion taux amélioré à 5.26
Renaming Columns
Glissez pour afficher le menu
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:
123456789import 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)
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:
12345678910111213import 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)
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:
123456789101112131415161718192021import 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)
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:
123456789101112import 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)
All these operations affect only the column names, not the data stored in the columns.
Merci pour vos commentaires !