0% found this document useful (0 votes)
9 views10 pages

Introduction To Pandas in Python

The document provides an introduction to the Pandas library in Python, highlighting its key features for data analysis and manipulation, such as reading/writing data, data cleaning, and basic operations. It explains the main data structures, Series and DataFrame, and includes practical examples and mini projects for analyzing student performance and employee salaries. Additionally, it covers advanced functionalities like filtering, grouping, and merging data, along with exporting results.

Uploaded by

shivanshjha973
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Introduction To Pandas in Python

The document provides an introduction to the Pandas library in Python, highlighting its key features for data analysis and manipulation, such as reading/writing data, data cleaning, and basic operations. It explains the main data structures, Series and DataFrame, and includes practical examples and mini projects for analyzing student performance and employee salaries. Additionally, it covers advanced functionalities like filtering, grouping, and merging data, along with exporting results.

Uploaded by

shivanshjha973
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Pandas in Python

1. What is Pandas?
Pandas is a fast, powerful, easy-to-use data analysis and manipulation library for Python.
It is mainly used when working with tabular data (rows & columns), similar to Excel spreadsheets.

�Key Pandas Features:

 Reading and writing data (CSV, Excel, SQL, JSON…)


 Data cleaning (handling missing values)
 Filtering and selecting data
 Grouping and aggregating
 Merging and joining tables
 Time series operations

�2. Pandas Main Data Structures


A. Series
A one-dimensional labeled array (like a single column).

import pandas as pd

s = [Link]([10, 20, 30, 40])


print(s)

B. DataFrame
A two-dimensional table with rows & columns (like Excel).

data = {
"Name": ["Aman", "Riya", "John"],
"Marks": [85, 90, 78]
}

df = [Link](data)
print(df)

�3. Reading Data with Pandas


Read CSV

df = pd.read_csv("[Link]")
Read Excel

df = pd.read_excel("[Link]")

�4. Basic DataFrame Operations


View Data
[Link]() # first 5 rows
[Link]() # last 5 rows
[Link]() # summary
[Link]() # stats summary

Select Columns
df["Name"]
df[["Name", "Marks"]]

Filter Rows
df[df["Marks"] > 80]

Add New Column


df["Grade"] = ["A", "A+", "B"]

Drop Column
[Link]("Grade", axis=1)

�5. Handling Missing Data


Check missing:
[Link]().sum()

Fill missing:
[Link](0)

Drop missing rows:


[Link]()
�6. Sorting & Grouping
Sorting
df.sort_values(by="Marks", ascending=False)

Grouping
[Link]("Class")["Marks"].mean()

�7. Merging & Joining DataFrames


merged_df = [Link](df1, df2, on="StudentID")

�8. Exporting Data


df.to_csv("[Link]", index=False)
df.to_excel("[Link]", index=False)

�MINI PROJECT: Student Performance Analyzer


A small real-world project using Pandas.

�Problem Statement
You have a dataset [Link] containing:

Name Class Maths Science English


Aman 10 78 88 74
Riya 10 92 95 90
John 9 65 70 68
Sana 9 81 76 84

You need to perform:

1. Load data
2. Calculate total & average
3. Assign grade
4. Find toppers
5. Export final report
�Solution Code
import pandas as pd

# 1. Load CSV
df = pd.read_csv("[Link]")

# 2. Calculate Total & Average


df["Total"] = df["Maths"] + df["Science"] + df["English"]
df["Average"] = df["Total"] / 3

# 3. Assign Grades
def grade(avg):
if avg >= 90:
return "A+"
elif avg >= 75:
return "A"
elif avg >= 60:
return "B"
else:
return "C"

df["Grade"] = df["Average"].apply(grade)

# 4. Find Class Toppers


toppers = df.sort_values(by="Total", ascending=False).head(3)

print("Top 3 Students:")
print(toppers)

# 5. Export Final Report


df.to_excel("Student_Report.xlsx", index=False)

�Output Example
Top 3 Students:

Name Total Average Grade


Riya 277 92.33 A+
Sana 241 80.33 A
Aman 240 80.0 A

�More Pandas Examples


�Example 1: Filtering with multiple conditions
import pandas as pd

df = [Link]({
"Name": ["Aman", "Riya", "John", "Sana"],
"Marks": [78, 92, 65, 81],
"City": ["Delhi", "Mumbai", "Delhi", "Kolkata"]
})

# Students scoring above 80 AND living in Delhi


filtered = df[(df["Marks"] > 80) & (df["City"] == "Delhi")]
print(filtered)

�Example 2: Rename Columns


[Link](columns={"Marks": "Score"}, inplace=True)

�Example 3: Replace Values


df["City"] = df["City"].replace({"Delhi": "New Delhi"})

�Example 4: Convert Data Types


df["Marks"] = df["Marks"].astype(float)

�Example 5: Grouping Multiple Columns


sales = [Link]({
"Region": ["North", "North", "South", "South"],
"Month": ["Jan", "Feb", "Jan", "Feb"],
"Sales": [20000, 22000, 18000, 25000]
})

result = [Link](["Region", "Month"])["Sales"].sum()


print(result)

�Example 6: Using apply() for row-wise calculations


df["Result"] = [Link](lambda row: "Pass" if row["Marks"] >= 40 else "Fail", axis=1)

�Example 7: Sorting by multiple columns


df.sort_values(by=["City", "Marks"], ascending=[True, False])

�Example 8: Read only selected columns from CSV


df = pd.read_csv("[Link]", usecols=["Name", "Marks"])

�Example 9: Drop duplicate entries


df.drop_duplicates(inplace=True)

�Example 10: Convert DataFrame to Dictionary


data_dict = df.to_dict(orient="records")
print(data_dict)
�Mini Project 1: Employee Salary Analyzer
�Objective

Analyze employee data:

 Calculate yearly salary


 Find highest-paid and lowest-paid employee
 Group employees by department
 Identify average salary per department
 Export report

�Dataset ([Link])

Name Department Salary Experience


Raj IT 45000 3
Priya HR 38000 2
John IT 52000 5
Asha Sales 30000 1

�Solution Code

import pandas as pd

df = pd.read_csv("[Link]")

# 1. Calculate yearly salary


df["Yearly Salary"] = df["Salary"] * 12

# 2. Find highest & lowest paid employees


highest = df.sort_values("Yearly Salary", ascending=False).head(1)
lowest = df.sort_values("Yearly Salary").head(1)

print("Highest Paid Employee:")


print(highest)

print("Lowest Paid Employee:")


print(lowest)

# 3. Average salary per department


avg_dept = [Link]("Department")["Salary"].mean()
print("\nAverage Salary per Department:")
print(avg_dept)

# 4. Export final report


df.to_excel("Employee_Salary_Report.xlsx", index=False)
�Mini Project 2: Sales Data Dashboard (Pandas +
Analysis Only)
�Objective

Analyze monthly sales data:

 Total yearly sales


 Best month
 Worst month
 Growth percentage month-to-month
 Product-wise contribution

�Dataset ([Link])

Month Product Quantity Price


Jan Laptop 30 45000
Jan Mobile 80 15000
Feb Laptop 25 46000
Feb Mobile 90 15500

�Solution Code

import pandas as pd

df = pd.read_csv("[Link]")

# 1. Calculate revenue
df["Revenue"] = df["Quantity"] * df["Price"]

# 2. Total revenue of the year


total_revenue = df["Revenue"].sum()
print("Total Revenue:", total_revenue)

# 3. Revenue per month


rev_month = [Link]("Month")["Revenue"].sum()
print("\nRevenue Per Month:")
print(rev_month)

# 4. Best & Worst months


best_month = rev_month.idxmax()
worst_month = rev_month.idxmin()

print("\nBest Month:", best_month)


print("Worst Month:", worst_month)

# 5. Product-wise revenue
product_rev = [Link]("Product")["Revenue"].sum()
print("\nProduct-wise Revenue:")
print(product_rev)

# 6. Export summary
summary = [Link]({
"Total_Revenue": [total_revenue],
"Best_Month": [best_month],
"Worst_Month": [worst_month]
})

summary.to_excel("Sales_Summary.xlsx", index=False)

You might also like