← Back to Python Course | Chapter 13: Data Science & Web | Lesson 4 of 14

Python Pandas DataFrame

DataFrame labeled columns वाली एक table है, जैसे आपके program के अंदर का spreadsheet। आप इसे edit कर सकते हैं, sort कर सकते हैं, खाली जगहें भर सकते हैं, और tables को जोड़ सकते हैं।
Syntax
python
import pandas as pd

df = pd.DataFrame({"column1": [a, b], "column2": [c, d]})
df["column"]
df.loc[row_label]

Columns को Edit करना

एक नया column जोड़ना उतना ही आसान है जितना किसी नए key को assign करना, जैसे df[total] = df[price] * df[qty], जो एक साथ हर row पर element-wise गणना करता है।

मौजूदा column बदलना भी उसी तरह काम करता है, और df.drop(columns=[...]) उन columns को हटा देता है जिनकी अब ज़रूरत नहीं।

उदाहरण: Editing Columns

python
import pandas as pd
df = pd.DataFrame({"price": [10, 20], "qty": [2, 3]})
df["total"] = df["price"] * df["qty"]  # new column computed element-wise
print(df)

Missing Data संभालना

Real-world data शायद ही कभी पूरा होता है: df.isna() missing values को flag करता है, df.dropna() उन्हें रखने वाली rows या columns हटा देता है, और df.fillna(value) उन्हें किसी दिए value या column mean जैसे किसी calculated statistic से replace कर देता है।

हटाने और भरने के बीच चुनाव इस पर निर्भर करता है कि missing data सुरक्षित रूप से छोड़े जाने लायक इतना कम है या नहीं।

उदाहरण: Handling Missing Data

python
import pandas as pd
import numpy as np
df = pd.DataFrame({"value": [1, np.nan, 3]})  # np.nan represents a missing value
print(df.fillna(0))  # replaces missing values with 0

DataFrames को Sort करना

df.sort_values(column) एक या ज़्यादा बताए गए columns की values के आधार पर DataFrame की rows को दोबारा क्रमबद्ध करता है, default रूप से ascending, और descending order के लिए एक ascending=False option के साथ।

यह अलग-अलग values की बजाय पूरी labeled rows पर काम करने वाला Python के sorted() का DataFrame-equivalent है।

उदाहरण: Sorting DataFrames

python
import pandas as pd
df = pd.DataFrame({"score": [70, 90, 50]})
print(df.sort_values("score"))  # rows reordered ascending by score

Data को Group करना

df.groupby(column) DataFrame को उस column में एक जैसा value साझा करने वाले groups में बाँट देता है, और बाद में .mean() या .sum() जैसा कोई aggregation chain करना उस statistic को हर group के भीतर अलग-अलग calculate करता है।

यह split-apply-combine pattern category के हिसाब से data summarize करने का मुख्य tool है।

उदाहरण: Grouping Data

python
import pandas as pd
df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team").mean())  # average score computed separately per team

Merging और Joining

pd.concat() कई DataFrames को एक साथ जोड़ता है (rows या columns के हिसाब से), जबकि pd.merge() उन्हें किसी साझा key column में मेल खाती values के आधार पर जोड़ता है, कुछ-कुछ SQL join जैसा।

जब भी related data कई tables में बँटा हो और analysis के लिए combine करना हो, merging ज़रूरी हो जाता है।

उदाहरण: Merging and Joining

python
import pandas as pd
a = pd.DataFrame({"id": [1, 2], "name": ["Alex", "Sam"]})
b = pd.DataFrame({"id": [1, 2], "score": [90, 80]})
print(pd.merge(a, b, on="id"))  # joins the two tables on the shared id column
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. #}

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.