Basic Data Science
Pratical
#Create 10 People Table
Code (In Excel Format)
import pandas as pd
# Data ko dictionary ke form me likhna
data = {
"Sr. No.": [1,2,3,4,5,6,7,8,9,10],
"Customer ID":
["CUST0001","CUST0002","CUST0003","CUST0004","CUST0005",
"CUST0006","CUST0007","CUST0008","CUST0009","CUST0010"],
"First Name": ["Alice","Brian","Cynthia","David","Emma",
"Felix","Grace","Henry","Isabella","Jack"],
"Last Name": ["Carter","Gomez","Lee","Singh","Johnson",
"Martinez","Kim","Wilson","Brown","Davis"],
"Email":
["[Link]@[Link]","[Link]@[Link]","[Link]
@[Link]",
"[Link]@[Link]","[Link]@[Link]","[Link]
nez@[Link]",
"[Link]@[Link]","[Link]@[Link]","[Link]
n@[Link]",
"[Link]@[Link]"],
"Phone Number":
[9403923487,9325267865,9246612243,9167956621,9089300999,
9010645377,8931989755,8853334133,8774678511,869022889],
"Address": ["101 Maple Street","202 Oak Avenue","303 Pine
Lane","404 Birch Blvd",
"505 Cedar Drive","606 Walnut Street","707 Chestnut
Court","808 Spruce Trail",
"909 Aspen Way","1010 Fir Terrace"],
"City": ["Canada","Austrialia","Seattle","Mumbai","Chicago",
"Phoenix","Poland","Atlanta","Boston","California"],
"Product/Item": ["Purchased laptop and accessories","Ordered
office chair",
"Bought desk, monitor, and mouse","Ordered
headphones and webcam",
"Purchased printer and ink","Bought smart home
devices",
"Ordered phone and case","Purchased tablet and
keyboard",
"Bought gaming console and games","Ordered TV
and sound system"],
"Quantity": [8,7,9,3,6,8,9,3,2,9],
"Price":
[26000,30000,34000,38000,42000,46000,50000,54000,58000,620
00],
"Purchase History": ["Good","Not Good","Better","Best","Good",
"Not Good","Better","Best","Good","Not Good"],
"Payment Method": ["Credit Card","PayPal","Debit Card","Bank
Transfer","Credit Card",
"PayPal","Debit Card","Cash","Credit Card","Google
Pay"]
}
# DataFrame banana
df = [Link](data)
# Excel me save karna
df.to_excel("[Link]", index=False)
print("Excel file '[Link]' Save.")
#Output :-
# This Code Shows Where Your File is
Saved.
#save file
import os
print([Link]())
#(EX.., # output :- C:\Users\Vishal )
# Solve This Questions
Database Name Customer_Details
1 ) Reading and Inspecting Data :
Q.1 ) How would you load a dataset (e.g., CSV, Excel)
into a Pandas DataFrame ?
Ans :- import pandas as pd
# CSV file load karne ke liye
#df = pd.read_csv("Customer_Details.csv")
# Excel file load
df = pd.read_excel("[Link]")
Q.2 ) How do you read a CSV file into a Pandas
DataFrame?
Ans :- To see the output of this query you need to
create a csv file, then you can read the file.
# read this file
df = pd.read_csv("[Link]")
Q.3 ) How do you display the first 5 rows of a
DataFrame?
Ans :- print([Link]()) # default 5 rows
print([Link](2)) # first 2 rows
# Output :-
[Link] Customer_ID First_Name Last_Name Email Phone_Number
Address City
0 CUST0001 Alice Carter [Link]@[Link] 9403923487
101 Maple Street Canada
1 CUST0002 Brian Gomez [Link]@[Link] 9325267865
202 Oak Avenue Australia
2 CUST0003 Cynthia Lee [Link]@[Link] 9246612243
303 Pine Lane Seattle
3 CUST0004 David Singh [Link]@[Link] 9167956621
404 Birch Blvd Mumbai
4 CUST0005 Emma Johnson [Link]@[Link] 9089300999
505 Cedar Drive Chicago
Product_Item Quantity Price Purchase_History
Payment_Method
Purchased laptop and accessories 8 26000 Good Credit Card
Ordered office chair 7 30000 Not Good PayPal
Bought desk, monitor, and mouse 9 34000 Better Debit Card
Ordered headphones and webcam 3 38000 Best Bank Transfer
Purchased printer and ink 6 42000 Good Credit Card
Q.4 ) How do you check the data types of columns in a
DataFrame ?
Ans :- print([Link])
#Output :-
Customer_ID object
First_Name object
Last_Name object
Email object
Phone_Number int64
Address object
City object
Product_Item object
Quantity int64
Price int64
Purchase_History object
Payment_Method object
dtype: object
Q.5 ) How do you get a summary of descriptive statistics
for numerical columns?
Ans :- print([Link]())
#Output :-
Phone_Number Quantity Price
count 1.000000e+01 10.00000 10.000000
mean 9.049973e+09 6.40000 44000.000000
std 2.381417e+08 2.75681 12110.601416
min 8.696023e+09 2.00000 26000.000000
25% 8.872998e+09 3.75000 35000.000000
50% 9.049973e+09 7.50000 44000.000000
75% 9.226948e+09 8.75000 53000.000000
max 9.403923e+09 9.00000 62000.000000
2 ) Handling Missing Values :
Q.1 ) How do you identify missing values in a
DataFrame?
Ans :- print([Link]().sum())
#Output :-
Customer_ID 0
First_Name 0
Last_Name 0
Email 0
Phone_Number 0
Address 0
City 0
Product_Item 0
Quantity 0
Price 0
Purchase_History 0
Payment_Method 0
dtype: int64
Q.2 ) How do you fill missing values with a specific value
(e.g., mean, median, mode)?
Ans :- import pandas as pd
# Example DataFrame (missing values)
data = {
"Quantity": [8, 7, None, 3, 6, None, 9, 3, None, 9],
"Price": [26000, None, 34000, 38000, 42000, 46000, None,
54000, 58000, 62000],
"City": ["Canada", "Australia", None, "Mumbai", "Chicago",
"Phoenix", "Poland", None, "Boston", "California"]
}
df = [Link](data)
print("# Before filling missing values:")
print(df)
# ✅ Mean se fill karna (Quantity column)
df["Quantity"] = df["Quantity"].fillna(df["Quantity"].mean())
# ✅ Median se fill karna (Price column)
df["Price"] = df["Price"].fillna(df["Price"].median())
# ✅ Mode se fill karna (City column)
df["City"] = df["City"].fillna(df["City"].mode()[0])
print("\n# After filling missing values:")
print(df)
#Output :-
# Before filling missing values: # After
filling missing values:
Quantity Price City Quantity
Price City
8.0 26000.0 Canada 8.000000
26000.0 Canada
7.0 NaN Australia
7.000000 44000.0 Australia
NaN 34000.0 None 6.428571
34000.0 Australia
3.0 38000.0 Mumbai 3.000000
38000.0 Mumbai
6.0 42000.0 Chicago
6.000000 42000.0 Chicago
NaN 46000.0 Phoenix 5
6.428571 46000.0 Phoenix
9.0 NaN Poland
9.000000 44000.0 Poland
3.0 54000.0 None
3.000000 54000.0 Australia
NaN 58000.0 Boston
6.428571 58000.0 Boston
9.0 62000.0 California 9
9.000000 62000.0 California
Q.3 ) How do you drop rows or columns containing
missing values?
Ans :- import pandas as pd
# Example DataFrame (with missing values)
data = {
"Quantity": [8, 7, None, 3, 6, None, 9, 3, None, 9],
"Price": [26000, None, 34000, 38000, 42000, 46000, None,
54000, 58000, 62000],
"City": ["Canada", "Australia", None, "Mumbai", "Chicago",
"Phoenix", "Poland", None, "Boston", "California"]
}
df = [Link](data)
print("# Original DataFrame (with NaN):")
print(df)
# ✅ Drop rows containing anNaN values
df_rows_dropped = [Link]()
# ✅ Drop columns containing any NaN values
df_cols_dropped = [Link](axis=1)
print("\n# After dropping rows with NaN values:")
print(df_rows_dropped)
print("\n# After dropping columns with NaN values:")
print(df_cols_dropped)
#Output :-
# Original DataFrame (with NaN):
Quantity Price City
8.0 26000.0 Canada
7.0 NaN Australia
NaN 34000.0 None
3.0 38000.0 Mumbai
6.0 42000.0 Chicago
NaN 46000.0 Phoenix
9.0 NaN Poland
3.0 54000.0 None
NaN 58000.0 Boston
9.0 62000.0 California
# After dropping rows with NaN values:
Quantity Price City
8.0 26000.0 Canada
3.0 38000.0 Mumbai
6.0 42000.0 Chicago
9.0 62000.0 California
# After dropping columns with NaN values:
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3 ) Data Visualization (Matplotlib):
Q.1 ) How do you create a line plot to visualize trends
over time?
Ans :- import [Link] as plt
[Link](df["Customer ID"], df["Quantity"], marker="o",
color="blue")
[Link]("Quantity Trend by Customer ID")
[Link]("Customer ID")
[Link]("Quantity")
[Link](True)
[Link]()
#Output :-
Q.2 How do you create a bar chart to compare
categories?
Ans :- city_sales = [Link]("City")["Price"].sum()
[Link](city_sales.index, city_sales.values, color="orange")
[Link]("Total Sales by City")
[Link]("City")
[Link]("Total Sales")
[Link]()
#Output :-
Q.3 ) How do you create a histogram to visualize the
distribution of a numerical variable?
Ans :- [Link](df["Price"], bins=5, color="green",
edgecolor="black")
[Link]("Distribution of Price")
[Link]("Price Range")
[Link]("Frequency")
[Link]()
#Output :-
Q.4 ) How do you create a scatter plot to examine the
relationship between two numerical variables?
Ans :- import [Link] as plt
[Link](figsize=(8,6))
[Link](df['Quantity'], df['Price'], alpha=0.6, c='blue',
edgecolor='k')
[Link]("Scatter Plot: Quantity vs Price")
[Link]("Quantity")
[Link]("Price")
[Link](True)
[Link]()
#Output :-
Q.5 ) How do you add labels, titles, and legends to a
plot?
Ans :- [Link](df["Customer ID"], df["Quantity"],
label="Quantity", marker="o")
[Link](df["Customer ID"], df["Price"], label="Price", marker="s")
[Link]("Quantity and Price by Customer ID")
[Link]("Customer ID")
[Link]("Values")
[Link]()
[Link]()
#Output :-
Q.6 ) How do you change the color, style, and markers of
plot elements?
Ans :- [Link](df["Customer ID"], df["Quantity"],
color="purple", linestyle="--", marker="s", markersize=8)
[Link]("Customized Quantity Plot")
[Link]("Customer ID")
[Link]("Quantity")
[Link]()
#Output :-
Q.7 ) How do you adjust the figure size and layout?
Ans :- [Link](figsize=(8,5))
[Link](df["Customer ID"], df["Price"], color="green",
marker="o")
[Link]("Price Trend with Figure Size Adjusted")
[Link]()
#Output :-
Q.8) How to create a scatter plot to visualize the
relationship between two numerical columns. Columnl
and column2 using Matplotlib.
Ans :- [Link](df["Quantity"], df["Price"], color="blue")
[Link]("Scatter Plot: Quantity vs Price")
[Link]("Quantity")
[Link]("Price")
[Link]()
#Output :-
4 ) Data analysis and exploration
Q.1 ) How would you calculate the mean, median, and
mode for a specific numerical column?
Ans :- mean_val = df["Price"].mean()
median_val = df["Price"].median()
mode_val = df["Price"].mode()[0]
print("Mean Price:", mean_val)
print("Median Price:", median_val)
print("Mode Price:", mode_val)
#Output :- Mean Price: 44000.0
Median Price: 44000.0
Mode Price: 26000
Q.2 ) How can you group data in a Pandas DataFrame
and calculate the sum of sales for each product
category?
Ans :- city_sales = [Link]("City")["Price"].sum()
print(city_sales)
#Output :-
City
Atlanta 54000
Austrialia 30000
Boston 58000
California 62000
Canada 26000
Chicago 42000
Mumbai 38000
Phoenix 46000
Poland 50000
Seattle 34000
Name: Price, dtype: int64
Q.3 ) How would you create a simple bar plot using
Matplotlib to visualize the distribution of a categorical
variable in a DataFrame?
Ans :- city_sales = [Link]("City")["Price"].sum()
[Link](city_sales.index, city_sales.values, color="skyblue")
[Link]("Sales Distribution by City")
[Link]("City")
[Link]("Total Sales")
[Link]()
#Output :-
# Error & Solution
# Error :-
Name ‘df’ is not defined
# Solution :-
Step 1.) Library Import
import pandas as pd
Step 2.) Excel File Load
file_path = "[Link]" # Excel file name
df = pd.read_excel(file_path)
Step 3.) Table check
print([Link]()) # first 5 rows
print([Link]) # rows, columns count