Python Pandas DataFrame
In this page:
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
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
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
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
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
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
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB