← Back to Pandas Course | Chapter 3: DataFrame Basics | Lesson 6 of 7

Columns का नाम बदलना

एक mapping से columns rename करें, या एक नई list assign करके एक साथ सारे names replace करें।
Syntax
python
df = df.rename(columns={"old_name": "new_name"})

Columns Rename करना

rename(columns={"old": "new"}) selected labels बदलता है और एक नया DataFrame return करता है। df.columns = [...] assign करना सारे names replace करता है और exact संख्या चाहता है। df.columns पर string methods, जैसे str.lower(), names को bulk में clean करते हैं।

Note: df.columns.str.strip().str.lower() messy headers standardize करता है।

उदाहरण: Renaming columns

python
import pandas as pd

df = pd.DataFrame({" First Name ": ["Ann"], "AGE": [28]})
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
print(df.columns.tolist())
print(df.rename(columns={"age": "years"}))

# Output:
# ['first_name', 'age']
#   first_name  years
# 0        Ann     28
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि rename एक copy return करता है
  2. names की गलत संख्या provide करना
  3. headers में stray spaces छोड़ना
चैप्टर सारांश
  • एक dict के साथ rename कुछ labels बदलता है
  • columns = [...] सारे replace करता है
  • String methods headers bulk में clean करते हैं
  • rename एक copy return करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.