0
0
Pandasdata~15 mins

Renaming columns in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Renaming columns
📖 Scenario: You have a small table of sales data with column names that are not very clear. You want to rename the columns to make them easier to understand.
🎯 Goal: Rename the columns of a pandas DataFrame to more descriptive names.
📋 What You'll Learn
Create a pandas DataFrame with specific columns and data
Create a dictionary to map old column names to new column names
Use the pandas rename method with the dictionary to rename columns
Print the resulting DataFrame
💡 Why This Matters
🌍 Real World
Renaming columns is common when working with data from different sources that use unclear or inconsistent column names.
💼 Career
Data scientists and analysts often rename columns to make data easier to understand and to prepare it for analysis or reporting.
Progress0 / 4 steps
1
Create the initial DataFrame
Create a pandas DataFrame called df with these columns and data: 'prd' with values [101, 102, 103], 'qty' with values [5, 10, 15], and 'amt' with values [100, 200, 300].
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Create the renaming dictionary
Create a dictionary called rename_dict that maps the old column names to new names: 'prd' to 'ProductID', 'qty' to 'Quantity', and 'amt' to 'Amount'.
Pandas
Need a hint?

Create a dictionary with old column names as keys and new column names as values.

3
Rename the columns using the dictionary
Use the pandas rename method on df with the columns=rename_dict argument and inplace=True to rename the columns.
Pandas
Need a hint?

Use df.rename(columns=rename_dict, inplace=True) to rename columns directly in the DataFrame.

4
Print the renamed DataFrame
Print the DataFrame df to see the new column names.
Pandas
Need a hint?

Use print(df) to display the DataFrame with renamed columns.