panda
panda
Source code
import pandas as pd
temperature = pd.Series([30, 32, 29, 31, 28], index=['Mon', 'Tue', 'Wed',
'Thu', 'Fri'])
print("Temperature Series:\n", temperature)
Output
PRACTICAL-2
Aim: To rename the columns of a DataFrame.
Source code
import pandas as pd
data = {'ProductID': [101, 102, 103], 'Price': [250, 450, 350]}
df = pd.DataFrame(data)
# Rename columns
df.rename(columns={'ProductID': 'ID', 'Price': 'Cost'}, inplace=True)
print("Renamed DataFrame:\n", df)
Output
PRACTICAL-3
Aim: To drop a specific column from a DataFrame.
Source code
import pandas as pd
data = {'Name': ['John', 'Paul', 'George'], 'Age': [29, 34, 31], 'City':
['Delhi', 'Mumbai', 'Pune']}
df = pd.DataFrame(data)
# Drop the 'City' column
df = df.drop('City', axis=1)
print("DataFrame after dropping 'City':\n", df)
Output
PRACTICAL-4
Aim: To calculate basic statistics like mean, median, and
standard deviation.
Source code
import pandas as pd
data = {'Scores': [85, 90, 78, 88, 92]}
df = pd.DataFrame(data)
# Calculate statistics
mean = df['Scores'].mean()
median = df['Scores'].median()
std_dev = df['Scores'].std()
print(f"Mean: {mean}, Median: {median}, Standard Deviation: {std_dev}")
Output
PRACTICAL-5
Aim: To concatenate two DataFrames vertically.
Source code
import pandas as pd
df1 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Subject': ['Math', 'Science']})
df2 = pd.DataFrame({'Name': ['Charlie', 'David'], 'Subject': ['History',
'Geography']})
# Concatenate DataFrames
result = pd.concat([df1, df2])
print("Concatenated DataFrame:\n", result)
Output
PRACTICAL-6
Aim: To create a pivot table to analyze data.
Source code
import pandas as pd
data = {'Employee': ['A', 'B', 'A', 'B'], 'Department': ['HR', 'HR', 'IT', 'IT'], 'Salary':
[50000, 52000, 60000, 62000]}
df = pd.DataFrame(data)
# Create a pivot table
pivot = pd.pivot_table(df, values='Salary', index='Department',
columns='Employee')
print("Pivot Table:\n", pivot)
Output
PRACTICAL-7
Aim: To remove duplicate rows from a DataFrame.
Source code
import pandas as pd
data = {'Name': ['Rita', 'Sita', 'Rita'], 'Subject': ['Math', 'Science', 'Math'], 'Marks':
[85, 90, 85]}
df = pd.DataFrame(data)
# Drop duplicate rows
df = df.drop_duplicates()
print("DataFrame after removing duplicates:\n", df)
Output
PRACTICAL-8
Aim: To replace specific values in a DataFrame.
Source code
import pandas as pd
data = {'Name': ['Ajay', 'Vijay', 'Sanjay'], 'Status': ['Fail', 'Pass', 'Fail']}
df = pd.DataFrame(data)
# Replace 'Fail' with 'Pending'
df['Status'] = df['Status'].replace('Fail', 'Pending')
print("Updated DataFrame:\n", df)
Output
PRACTICAL-9
Aim: To create and display a time series using Pandas.
Source code
import pandas as pd
# Create a time series
date_range = pd.date_range(start='2023-01-01', periods=5)
data = pd.Series([10, 15, 20, 25, 30], index=date_range)
print("Time Series:\n", data)
Output
PRACTICAL-10
Aim: To create a Pandas Series using a dictionary and
display its contents.
Source code
import pandas as pd
# Creating a Series using a dictionary
student_scores = {'John': 85, 'Alice': 92, 'Bob': 78, 'Emma': 88}
series = pd.Series(student_scores)
print("Pandas Series from Dictionary:")
print(series)
Output
PRACTICAL-11
Aim: To merge two DataFrames based on a common column.
Source code
import pandas as pd
# First DataFrame
data1 = {'ID': [1, 2, 3], 'Name': ['Alice', 'Bob', 'Charlie']}
df1 = pd.DataFrame(data1)
# Second DataFrame
data2 = {'ID': [2, 3, 4], 'Score': [85, 90, 88]}
df2 = pd.DataFrame(data2)
# Merging DataFrames on 'ID'
merged_df = pd.merge(df1, df2, on='ID', how='inner')
print("Merged DataFrame:")
print(merged_df)
Output
PRACTICAL-12
Aim: To group data by a category and calculate aggregate
statistics.
Source code
import pandas as pd
# Creating a DataFrame with sales data
data = {'Category': ['Electronics', 'Clothing', 'Electronics', 'Groceries', 'Clothing',
'Groceries'],
'Sales': [1500, 800, 1200, 500, 700, 900],
'Profit': [200, 100, 150, 50, 70, 80]}
df = pd.DataFrame(data)
# Grouping by 'Category' and calculating total Sales and average Profit
aggregated_data = df.groupby('Category').agg({'Sales': 'sum', 'Profit': 'mean'})
print("Aggregated Data:")
print(aggregated_data)
Output
PRACTICAL-13
Aim: To create database
Source code
Create Databases general_store;
Output
PRACTICAL-14
Aim: To create Table
Source code
create table customer_record(CustomerName char(50),
CustomerAddress varchar(50), CustomerContact int(80),
Item char(60), AmountofPurchase int(60));
desc customer record;
Output
PRACTICAL-15
Aim: To insert the details in the table ‘customer_record’
Source code
insert into customer_record values("Ritik", "G-55", 98743456, "Dal", 200);
insert into customer_record values("yogesh", "G-56", 84937563, "soap", 30);
insert into customer_record values("Tannu", "G-45", 64854465, "chocolate", 100);
insert into customer_record values("harshita", "G-42",64876555, "biscuit", 80);
insert into customer_record values("shruti", "G-58", 86432223, "Noodles", 45);
Output
PRACTICAL-16
Aim: To show table “ customer_record ”
Source code
Select*from customer_record;
Output
PRACTICAL – 17
Aim: To add a new column in table.
Source code
Alter table customer_record add DueAmount int(80);
Output
PRACTICAL-18
Aim: To add details in a new column in table.
Source code
Update customer_record set DueAmount=0 where Item= “Dal”;
Update customer_record set DueAmount=5 where Item= “soap”;
Update customer_record set DueAmount=10 where Item= “chocolatel”;
Update customer_record set DueAmount=40 where Item= “biscuit”;
Update customer_record set DueAmount=15 where Item= “Noodles”;
Select*from customer_record;
Output
PRACTICAL-19
Aim: To delete a row from table.
Source code
Delete from customer_record where
CustomerName=”harshita”;
Output
PRACTICAL-20
Aim: To get the details of the customer with Due
Amount more than or equal to 5
Source code
Select*from customer_record where DueAmount >=5;
Output
PRACTICAL-21
Aim: To arrange the DueAmount in descending order.
Source code
Select *from customer_record order by DueAmount
DESC;
Output
PRACTICAL-22
Aim: Create a student table, insert records, and
calculate total and average marks using arithmetic
operations.
Source code
CREATE DATABASE School;
USE School;
Name VARCHAR(50),
Math INT,
Science INT,
English INT);
VALUES
SELECT
Roll_No,
Name,
FROM
Student;
OUTPUT
PRACTICAL-23
Aim: Create an employee table and calculate the total
salary of employees after adding a bonus.
Source code
CREATE TABLE Employee (
Emp_ID INT PRIMARY KEY,
Name VARCHAR(50),
Basic_Salary DECIMAL(10, 2),
Bonus_Percentage DECIMAL(5, 2));
INSERT INTO Employee (Emp_ID, Name, Basic_Salary, Bonus_Percentage)
VALUES
(101, 'Meena', 50000, 10),
(102, 'Raj', 60000, 15),
(103, 'Simran', 45000, 12);
SELECT
Emp_ID,
Name,
Basic_Salary,
Bonus_Percentage,
Basic_Salary + (Basic_Salary * Bonus_Percentage / 100) AS Total_Salary
FROM
Employee;
OUTPUT
PRACTICAL-24
Aim: Create a product table and calculate the price of
products after applying a discount.
Source code
CREATE TABLE Product (
Product_ID INT PRIMARY KEY,
Product_Name VARCHAR(50),
Price DECIMAL(10, 2),
Discount_Percentage DECIMAL(5, 2));
INSERT INTO Product (Product_ID, Product_Name, Price, Discount_Percentage)
VALUES
(1, 'Laptop', 50000, 10),
(2, 'Mobile', 20000, 5),
(3, 'Headphones', 3000, 20);
SELECT
Product_ID,
Product_Name,
Price,
Discount_Percentage,
Price - (Price * Discount_Percentage / 100) AS Discounted_Price
FROM
Product;
OUTPUT
PRACTICAL-25
Aim: To plot a simple line graph showing the
relationship between two variables.
Source code
# Data
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]
# Plotting the line graph
plt.plot(x, y, color='green', marker='o')
# Adding titles and labels
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Display the plot
plt.show()
Output
PRACTICAL-26
Aim: To create a bar chart representing data.
Source code
import matplotlib.pyplot as plt
# Data
categories = ['Math', 'Science', 'English', 'History', 'Geography']
values = [85, 90, 75, 80, 88]
# Plotting the bar chart
plt.bar(categories, values, color='skyblue')
# Adding titles and labels
plt.title('Subject Scores')
plt.xlabel('Subjects')
plt.ylabel('Scores')
# Display the plot
plt.show()
Output
PRACTICAL-27
Aim: To create a histogram showing the distribution of
data.
Source code
import matplotlib.pyplot as plt
# Data: Age distribution of students
ages = [12, 13, 14, 15, 16, 14, 15, 16, 17, 15, 16, 17, 18, 15, 16]
# Plotting the histogram
plt.hist(ages, bins=5, color='purple', edgecolor='black')
# Adding titles and labels
plt.title('Age Distribution of Students')
plt.xlabel('Age')
plt.ylabel('Frequency')
# Display the plot
plt.show()
Output
PRACTICAL-28
Aim: To plot a scatter graph to show the relationship
between two variables.
Source code
import matplotlib.pyplot as plt
# Data: Height vs Weight
height = [150, 160, 170, 180, 190]
weight = [50, 60, 70, 80, 90]
# Plotting the scatter plot
plt.scatter(height, weight, color='red')
# Adding titles and labels
plt.title('Height vs Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
# Display the plot
plt.show()
Output
PRACTICAL-29
Aim: To create a pie chart showing the percentage
distribution of categories.
Source code
import matplotlib.pyplot as plt
# Data
labels = ['Python', 'Java', 'C++', 'JavaScript']
sizes = [40, 30, 20, 10]
# Plotting the pie chart
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90,
colors=['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'])
# Adding title
plt.title('Programming Language Popularity')
# Display the plot
plt.show()
Output
PRACTICAL-30
Aim: To create a line plot and customize it by adding
grid, title, and labels.
Source code
import matplotlib.pyplot as plt
# Data: Sales over 5 months
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [5000, 7000, 8000, 6500, 9000]
# Plotting the line graph
plt.plot(months, sales, color='blue', marker='o')
# Customizing the plot
plt.title('Monthly Sales', fontsize=16)
plt.xlabel('Months', fontsize=12)
plt.ylabel('Sales (in INR)', fontsize=12)
plt.grid(True)
# Display the plot
plt.show()
Output