Columns जोड़ना/हटाना
एक नए नाम को assign करके नए columns बनाएँ और drop से old वालों को remove करें।
In this page:
Syntax
df["new_column"] = df["column1"] * df["column2"]
df = df.drop(columns=["column"])
Columns Add/Drop करना
दूसरों से compute किया एक column add करने के लिए df["new"] = ... assign करें। drop(columns=[...]) columns remove करता है और एक नया DataFrame return करता है जब तक inplace=True न हो। assign() columns add करता है और एक नया DataFrame return करता है, जो method chaining के लिए suit करता है।
Note:
inplace=True से df = df.drop(...) prefer करें; यह ज़्यादा clearly पढ़ा जाता है और बेहतर chain करता है।
उदाहरण: Adding/dropping columns
import pandas as pd
df = pd.DataFrame({"price": [10, 20], "qty": [3, 5]})
df["total"] = df["price"] * df["qty"]
print(df)
print(df.drop(columns=["qty"]))
print(df.assign(tax=df["total"] * 0.1))
# Output:
# price qty total
# 0 10 3 30
# 1 20 5 100
# price total
# 0 10 30
# 1 20 100
# price qty total tax
# 0 10 3 30 3.0
# 1 20 5 100 10.0
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. #}
आम गलतियां
- यह भूल जाना कि drop एक नया DataFrame return करता है
- axis को incorrectly इस्तेमाल करना
- गलत length की एक list assign करना
चैप्टर सारांश
- एक column add करने के लिए assign करें
- drop(columns=...) remove करता है
- assign chaining support करता है
- List length rows से match होनी चाहिए
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: