Data Science & Machine Learning
June 13, 2025 at 07:55 PM
Today, let's move to the next topic in the Data Science Learning Series:
🔹 *Renaming Columns & Reindexing*
🧠 *Why Rename Columns?*
- To make column names more readable
- To standardize names (e.g., remove spaces, use lowercase)
- To match expected input format for ML models or APIs
✅ *How to Rename Columns in Pandas*
*1. Rename one or more columns*
df.rename(columns={'OldName': 'NewName'}, inplace=True)
*2. Rename all columns at once*
df.columns = ['col1', 'col2', 'col3', ...]
*3. Clean column names using a loop*
df.columns = [col.strip().lower().replace(" ", "_") for col in df.columns]
🔁 *What Is Reindexing*?
Reindexing means changing the row labels (index) of your DataFrame.
✅ *How to Reindex*
*1. Reset index (commonly used)*
df.reset_index(drop=True, inplace=True)
*2. Set a specific column as index*
df.set_index('CustomerID', inplace=True)
*3. Custom reindexing*
df.reindex([2, 0, 1]) # Reorders the rows
📊 *Real-Life Example*
In a dataset:
- Column "Customer ID" → rename to "customer_id" for consistency
- Reset index after cleaning and filtering for neat presentation
- Set "Date" as index when working with time-series data
*React with ❤️ once you're ready for the quiz*
Data Science Learning Series: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D/998
Python Cheatsheet: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1660
❤️
👍
❤
🙏
🇮🇳
🩲
☄️
♥
❣
🇵🇰
50