Data Manipulation with Pandas
Pandas is the backbone of data analytics with Python. It simplifies loading, cleaning,
and transforming structured datasets. If you’re following a python for data analysis tutorial, Pandas is where you’ll spend most of your time.
1️⃣ Loading Data
import pandas as pd
df_csv = pd.read_csv("sales.csv")
df_excel = pd.read_excel("sales.xlsx", sheet_name="Jan")
df_json = pd.read_json("config.json")
Pandas supports CSV, Excel, JSON, SQL, and more—ideal for real-world data.
2️⃣ Exploring Data
df.head()
df.info()
df.describe()
df.columns
Quickly inspect structure, stats, and column names before analysis.
3️⃣ Cleaning Data
df.dropna(inplace=True) # remove nulls
df.fillna(0, inplace=True) # replace nulls with 0
df["Sales"] = df["Sales"].astype(float)
Simple commands handle missing values and enforce correct datatypes.
4️⃣ Filtering Data
Select rows that meet certain conditions:
# Filter rows where 'Sales' > 1000
filtered_df = df[df["Sales"] > 1000]
5️⃣ Grouping Data
Split data into categories and compute statistics:
# Group by region and calculate average profit
grouped_df = df.groupby("Region")["Profit"].mean()
6️⃣ Aggregating Data
Summarize values by sum, count, or custom metrics:
# Sum of Sales by Region
aggregated_df = df.groupby("Region").agg({"Sales": "sum"})
7️⃣ Multiple Aggregations
Apply multiple aggregations simultaneously:
# Region-wise mean, sum, and count of Sales
agg_multi = df.groupby("Region").agg({
"Sales": ["mean", "sum", "count"]
})
8️⃣ Saving Data
df.to_csv("cleaned_sales.csv", index=False)
df.to_excel("cleaned_sales.xlsx", sheet_name="Cleaned")
Processed datasets can be exported for reporting or further analysis.
With Pandas, you can filter, group, and aggregate data with just a few lines of code.
Next, let’s visualize these insights using Matplotlib and Seaborn.