GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY
SHRI BALWANT INSTITUTE OF TECHNOLOGY
JAVA
PRACTICAL FILE
BCA-202
Submitted By: Submitted To:
Name: Nikhil Singh
Class: BCA IV SEM
Enrolment no:
INDEX
S. No. Detailed Statement Mapping
to CO #
1 Create a pandas series from a dictionary of values and a ndarray. CO1, CO3
2 Create a Series and print all the elements that are above 75 percentile. CO2, CO3
3 Perform sorting on Series data and DataFrames. CO2, CO3
4 Write a program to implement pivot() and pivot_table() on a CO2, CO3,
DataFrame. CO4
5 Write a program to find mean absolute deviation on a DataFrame. CO2, CO3,
CO4
6 Two Series objects, Population stores the details of four metro cities of CO2, CO3,
India and another object AvgIncome stores the total average income CO4
reported in four years in these cities. Calculate income per capita for
each of these metro cities.
7 Create a DataFrame based on E-Commerce data and generate mean, CO2, CO3,
mode, median. CO4
8 Create a DataFrame based on employee data and generate quartile and CO2, CO3,
variance. CO4
9 Program to implement skewness on random data. CO2, CO3,
CO4
10 Create a DataFrame on any data and compute statistical function of CO2, CO3,
kurtosis. CO4
11 Series objects Temp1, temp2, temp3, temp4 store the temperature of CO2, CO3,
days of week 1, week 2, week 3, week 4. Write a script to: - CO4
a. prints the average temperature per week.
b. Print average temperature of entire month.
12 Write a Program to read a CSV file and create its DataFrame. CO2, CO3,
CO4
13 Consider the DataFrame QtrSales where each row contains the item CO2, CO3,
category, item name, and expenditure and group the rows by category. CO4
Print the average expenditure per category.
14 Create a DataFrame having age, name, weight of five students. Write a CO2, CO3,
program to display only the weight of first and fourth rows. CO4
15 Write a program to create a DataFrame to store weight, age, and name CO2, CO3,
of three people. Print the DataFrame and its transpose. CO4
PROGRAM1
Aim: Create a pandas series from a dictionary of values and a ndarray.
CODE:
import pandas as pd
import numpy as np
# Creating a pandas series from a dictionary
data_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
series_from_dict = pd.Series(data_dict)
print("Series from dictionary:\n", series_from_dict)
# Creating a pandas series from an ndarray
data_ndarray = np.array([10, 20, 30, 40])
series_from_ndarray = pd.Series(data_ndarray)
print("\nSeries from ndarray:\n", series_from_ndarray)
OUTPUT:
PROGRAM2
Aim: Create a Series and print all the elements that are above 75 percentile.
CODE:
# Creating a pandas series
data_series = pd.Series([15, 50, 75, 85, 90, 95, 100])
# Calculating the 75th percentile
percentile_75 = np.percentile(data_series, 75)
print("75th Percentile:", percentile_75)
# Printing elements above the 75th percentile
above_75_percentile = data_series[data_series > percentile_75]
print("\nElements above 75th percentile:\n", above_75_percentile)
OUTPUT:
PROGRAM3
Aim: Perform sorting on Series data and DataFrames.
CODE:
# Sorting a Series
sorted_series = data_series.sort_values()
print("\nSorted Series:\n", sorted_series)
# Creating a DataFrame
df = pd.DataFrame({
'A': [3, 1, 2],
'B': [2, 3, 1],
'C': [1, 2, 3]})
# Sorting DataFrame by column 'A'
sorted_df = df.sort_values(by='A')
print("\nDataFrame sorted by column 'A':\n", sorted_df)
OUTPUT:
PROGRAM4
Aim: Write a program to implement pivot() and pivot_table() on a DataFrame.
CODE:
# Creating a DataFrame for pivot examples
data = {
'date': ['2023-01-01', '2023-01-01', '2023-01-02', '2023-01-02'],
'city': ['New York', 'Los Angeles', 'New York', 'Los Angeles'],
'temperature': [32, 75, 30, 70]
df = pd.DataFrame(data)
# Using pivot
pivot_df = df.pivot(index='date', columns='city', values='temperature')
print("\nPivoted DataFrame:\n", pivot_df)
# Using pivot_table
pivot_table_df = df.pivot_table(index='date', columns='city', values='temperature',
aggfunc='mean')
print("\nPivot Table DataFrame:\n", pivot_table_df)
OUTPUT:
PROGRAM5
Aim: Write a program to find mean absolute deviation on a DataFrame.
CODE:
# Creating a DataFrame
df_mad = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [5, 6, 7, 8, 9],
'C': [9, 8, 7, 6, 5]
})
# Calculating mean absolute deviation
mad = df_mad.mad()
print("\nMean Absolute Deviation:\n", mad)
OUTPUT:
PROGRAM6
Aim: Two Series objects, Population stores the details of four metro cities of India and
another object AvgIncome stores the total average income reported in four years in these
cities. Calculate income per capita for each of these metro cities.
CODE:
# Creating Series objects
Population = pd.Series([8.4, 4.0, 2.8, 2.1], index=['Delhi', 'Mumbai', 'Kolkata', 'Chennai'])
AvgIncome = pd.Series([60000, 50000, 45000, 40000], index=['Delhi', 'Mumbai', 'Kolkata',
'Chennai'])
# Calculating income per capita
income_per_capita = AvgIncome / Population
print("\nIncome per capita:\n", income_per_capita)
OUTPUT:
PROGRAM7
Aim: Create a DataFrame based on E-Commerce data and generate mean, mode, median.
CODE:
# Creating a DataFrame
ecommerce_data = pd.DataFrame({
'Item': ['A', 'B', 'C', 'A', 'B', 'C', 'A'],
'Price': [100, 150, 200, 100, 150, 200, 100]
})
# Calculating mean, mode, and median
mean_price = ecommerce_data['Price'].mean()
mode_price = ecommerce_data['Price'].mode()[0]
median_price = ecommerce_data['Price'].median()
print("\nMean price:", mean_price)
print("Mode price:", mode_price)
print("Median price:", median_price)
OUTPUT:
PROGRAM8
Aim: Create a DataFrame based on employee data and generate quartile and variance.
CODE:
# Creating a DataFrame
employee_data = pd.DataFrame({
'Age': [25, 30, 45, 35, 50],
'Salary': [50000, 60000, 80000, 70000, 90000]
})
# Calculating quartiles and variance
quartiles = employee_data.quantile([0.25, 0.5, 0.75])
variance = employee_data.var()
print("\nQuartiles:\n", quartiles)
print("\nVariance:\n", variance)
OUTPUT:
PROGRAM9
Aim: Program to implement skewness on random data.
CODE:
# Creating random data
random_data = np.random.randn(1000)
# Calculating skewness
skewness = pd.Series(random_data).skew()
print("\nSkewness of random data:", skewness)
OUTPUT:
PROGRAM10
Aim: Create a DataFrame on any data and compute statistical function of kurtosis.
CODE:
# Creating a DataFrame
data_kurtosis = pd.DataFrame({
'Data': np.random.randn(1000)
})
# Calculating kurtosis
kurtosis = data_kurtosis.kurtosis()
print("\nKurtosis:\n", kurtosis)
OUTPUT:
PROGRAM11
Aim: Series objects Temp1, temp2, temp3, temp4 store the temperature of days of week 1,
week 2, week 3, week 4. Write a script to: -
a. prints the average temperature per week.
b. Print average temperature of entire month.
CODE:
# Creating Series objects for temperatures
temp1 = pd.Series([30, 32, 35, 28, 25, 24, 26])
temp2 = pd.Series([29, 31, 33, 27, 26, 25, 30])
temp3 = pd.Series([28, 30, 32, 26, 24, 23, 29])
temp4 = pd.Series([27, 29, 31, 25, 23, 22, 28])
# Combining temperatures into a DataFrame
temps = pd.DataFrame({'Week1': temp1, 'Week2': temp2, 'Week3': temp3, 'Week4':
temp4})
# Calculating average temperature per week and for the entire month
avg_per_week = temps.mean()
avg_entire_month = temps.stack().mean()
print("\nAverage temperature per week:\n", avg_per_week)
print("\nAverage temperature of entire month:", avg_entire_month)
OUTPUT:
PROGRAM12
Aim: Write a Program to read a CSV file and create its DataFrame.
CODE:
# Reading a CSV file into a DataFrame
# df_csv = pd.read_csv('path_to_your_csv_file.csv')
# Example DataFrame for demonstration
df_csv = pd.DataFrame({
'Category': ['A', 'A', 'B', 'B', 'C'],
'Item': ['Item1', 'Item2', 'Item1', 'Item2', 'Item1'],
'Expenditure': [100, 200, 150, 250, 300]
})
print("\nCSV DataFrame:\n", df_csv)
OUTPUT:
PROGRAM13
Aim: Consider the DataFrame QtrSales where each row contains the item category, item
name, and expenditure and group the rows by category. Print the average expenditure per
category.
CODE:
# Grouping by category and calculating average expenditure
grouped = df_csv.groupby('Category')['Expenditure'].mean()
print("\nAverage expenditure per category:\n", grouped)
OUTPUT:
PROGRAM14
Aim: Create a DataFrame having age, name, weight of five students. Write a program to
display only the weight of first and fourth rows.
CODE:
# Creating a DataFrame
students_data = pd.DataFrame({
'Age': [20, 21, 22, 23, 24],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Weight': [55, 65, 75, 85, 95]
})
# Displaying weight of first and fourth rows
weights = students_data.loc[[0, 3], 'Weight']
print("\nWeights of first and fourth rows:\n", weights)
OUTPUT:
PROGRAM15
Aim: Write a program to create a DataFrame to store weight, age, and name of three people.
Print the DataFrame and its transpose.
CODE:
# Creating a DataFrame
people_data = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Weight': [55, 75, 65]
})
# Printing the DataFrame and its transpose
print("\nOriginal DataFrame:\n", people_data)
print("\nTransposed DataFrame:\n", people_data.T)
OUTPUT: