MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Renaming Columns, Replacing Values & Sorting0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
5.1Applying Custom Functions (apply, map, applymap)5.2Vectorized String Methods (.str accessor)5.3Renaming Columns, Replacing Values & Sorting
HomeTutorialsPandasRenaming Columns, Replacing Values & Sorting
GroupBy Mechanics: Split-Apply-Combine

Renaming Columns, Replacing Values & Sorting

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Renaming Columns & Index Labels (rename)

Often raw datasets come with messy column names containing spaces, typos, or special characters. Use df.rename():

1import pandas as pd
2 
3df = pd.DataFrame({
4 'emp id': [101, 102, 103],
5 'emp_name': ['Amit', 'Sunita', 'Karan'],
6 'sal': [50000, 75000, 62000]
7})
8 
9# Renaming specific columns using a dictionary
10df_renamed = df.rename(columns={
11 'emp id': 'Employee_ID',
12 'emp_name': 'Full_Name',
13 'sal': 'Salary_INR'
14})
15print(df_renamed)

Bulk Column Renaming (Sanitizing All Headers):

1# Clean all column names: lowercase and replace spaces with underscores
2df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_')

2. Replacing Values Across Columns (replace)

The .replace() method searches and replaces specific values across a Series or entire DataFrame:

1# Replace legacy department codes with full names
2df['Department'] = df['Department'].replace({
3 'MKT': 'Marketing',
4 'FIN': 'Finance',
5 'ENG': 'Engineering'
6})

3. Sorting Data in Pandas

A. Sorting by Column Values (sort_values)

1# Sort by Salary descending
2df_sorted = df.sort_values(by='Salary_INR', ascending=False)
3 
4# Multi-column sorting: First by Department (Ascending), then by Salary (Descending)
5df_multi_sort = df.sort_values(
6 by=['Department', 'Salary_INR'],
7 ascending=[True, False]
8)

B. Sorting by Index Labels (sort_index)

After filtering or grouping operations, restore original sequential index ordering:

1df_restored = df.sort_index(ascending=True)

4. Inspecting Extremes: nlargest & nsmallest

Instead of sorting a 10-million row table just to find the top 5 earners, use the optimized nlargest() method:

1# Top 5 highest earners
2top_5 = df.nlargest(5, 'Salary_INR')
3 
4# 3 lowest revenue products
5bottom_3 = df.nsmallest(3, 'Revenue')

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which parameter in df.sort_values() determines whether sorting is lowest-to-highest or highest-to-lowest?
Question 2
How can you rename the column 'old_col' to 'new_col' in a DataFrame?
Question 3
Which method efficiently retrieves the top 10 highest rows based on a specific column without sorting the entire dataset?
Question 4
If you want to sort by 'Department' ascending and 'Salary' descending simultaneously, what syntax is used?
Question 5
What does df.sort_index() do?

Next Lesson

GroupBy Mechanics: Split-Apply-Combine

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Vectorized String Methods (.str accessor)GroupBy Mechanics: Split-Apply-Combine

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext