Columns का नाम बदलना
एक mapping से columns rename करें, या एक नई list assign करके एक साथ सारे names replace करें।
In this page:
Syntax
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
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. #}
आम गलतियां
- यह भूल जाना कि rename एक copy return करता है
- names की गलत संख्या provide करना
- 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: