0% found this document useful (0 votes)
4 views24 pages

IP Practical Programs Complete

The document contains a series of programming tasks and examples related to SQL queries and Pandas operations in Python. It includes instructions for displaying, comparing, and manipulating data in tables and series, as well as creating dataframes for various applications. The document provides code snippets and expected outputs for each task.

Uploaded by

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

IP Practical Programs Complete

The document contains a series of programming tasks and examples related to SQL queries and Pandas operations in Python. It includes instructions for displaying, comparing, and manipulating data in tables and series, as well as creating dataframes for various applications. The document provides code snippets and expected outputs for each task.

Uploaded by

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

INDEX

S. Program Title
No.

Write an SQL query to display all the divnos from both the
1. tables.

Write an SQL query to display all the distinct divnos from


2. both the tables.

Write an SQL query to display all the divnos in Member table


3. but not in the Division table.

Write an SQL query to display all the divnos in Division table


4. but not in the Member table.

Write an SQL query to display common divnos from both the


5. tables.

6. Creating a Basic Non-Empty Series

7. Creating a Series using a String

8. Creating a Series using a List

9. Creating a Series using a Tuple

10. Creating a Series using a Python Dictionary

11. Creating a Series using a NumPy ndarray

12. Creating a Series using a Scalar Value

Q1: Series Object temperature storing temperatures for 7


13. days

Q2: Series Object section storing student count per Class 12


14. section

WAP to create a series from a dictionary of values and


15. ndarray.

Write a Pandas program to compare the elements of the two


16. Pandas Series.

Write a Pandas program to add, subtract, multiply and divide


17. two Pandas Series.
S. Program Title
No.

18. Write a program to sort the elements of Series S1 into S2.

Write a Pandas program to select the rows where the height


19. is not known, i.e. is NaN.

Write a Python program to convert a dictionary to a Pandas


20. series.

WAP to create a DataFrame of students (name, class, marks,


21. grade), display names, and calculate sum of subjects.

WAP to create a DataFrame for examination result and


22. display labels, data types, and dimensions.

WAP to create a DataFrame to store weight, age and name of


23. 5 people, and display details as described.

Create a dataframe with fruits, their quantity and their price,


24. calculate totals and display fruit details.

Create 2 dataframes of marks of five subjects of students and


25. add them, show total marks.

26. WAP to replace all negative values in a dataframe with a 0.

Program to store monthly sales of 5 items using 12 Series


27. objects (one for each month).

WAP to create a dataframe (prodf) with production of rice,


28. pulses, fruits, wheat for 5 different states.

Analyse student performance with school result data on


29. parameters (subject-wise or class-wise).

WAP to create scatter chart to display two students' scores


30. in 5 subjects.

WAP to create Line charts for plotting year-wise sales of TV


31. Brands A, B, C, D.

WAP to plot a Bar chart for the sales of TV of brand A in last


32. four years.

Plot amount collected for charity vs. days using a bar chart
33. with day labels.

34. From Data Frame pdas, line chart of 1990 & 2000 columns;
S. Program Title
No.

bar chart plotting all columns.

35. WAP to import data between pandas and CSV file.

36. WAP to export data from pandas to CSV file.

37. WAP to draw a histogram.

38. Menu Driven Program.


PANDAS

Q1) WAP to create a series from a dictionary of values and ndarray.


import pandas as pd
import numpy as np

# Dictionary to Series
d = {'a': 10, 'b': 20, 'c': 30}
s1 = [Link](d)

# ndarray to Series
arr = [Link]([10, 20, 30, 40])
s2 = [Link](arr)

print("Series from dictionary:")


print(s1)
print("\nSeries from ndarray:")
print(s2)

Output:

Series from dictionary:


a 10
b 20
c 30
dtype: int64

Series from ndarray:


0 10
1 20
2 30
3 40
dtype: int32

Q2) Write a Pandas program to compare the elements of the two


Pandas Series.
import pandas as pd

s1 = [Link]([1, 2, 3, 4])
s2 = [Link]([1, 3, 2, 4])

# Element-wise comparison
print("Equal:")
print(s1 == s2)
print("\nGreater:")
print(s1 > s2)
print("\nLess:")
print(s1 < s2)

Output:

Equal:
0 True
1 False
2 False
3 True
dtype: bool

Greater:
0 False
1 False
2 True
3 False
dtype: bool

Less:
0 False
1 True
2 False
3 False
dtype: bool

Q3) Write a Pandas program to add, subtract, multiply and divide two
Pandas Series.
import pandas as pd

s1 = [Link]([10, 20, 30])


s2 = [Link]([1, 2, 3])

print("Addition:")
print(s1 + s2)
print("\nSubtraction:")
print(s1 - s2)
print("\nMultiplication:")
print(s1 * s2)
print("\nDivision:")
print(s1 / s2)

Output:

Addition:
0 11
1 22
2 33
dtype: int64

Subtraction:
0 9
1 18
2 27
dtype: int64

Multiplication:
0 10
1 40
2 90
dtype: int64

Division:
0 10.0
1 10.0
2 10.0
dtype: float64
Q4) Write a program to sort the elements of Series S1 into S2.
import pandas as pd

s1 = [Link]([5, 2, 8, 1])
s2 = s1.sort_values()
print("Original Series:")
print(s1)
print("\nSorted Series:")
print(s2)

Output:

Original Series:
0 5
1 2
2 8
3 1
dtype: int64

Sorted Series:
3 1
1 2
0 5
2 8
dtype: int64

Q5) Write a Pandas program to select the rows where the height is
not known, i.e. is NaN.
import pandas as pd
import numpy as np

data = {
'name': ['Asha', 'Radha', 'Kamal', 'Divy', 'Anjali'],
'height': [5.5, 5, [Link], 5.9, [Link]],
'age': [11, 23, 22, 33, 22]
}
df = [Link](data)
print("Complete DataFrame:")
print(df)
print("\nRows where height is NaN:")
print(df[df['height'].isna()])

Output:

Complete DataFrame:
name height age
0 Asha 5.5 11
1 Radha 5.0 23
2 Kamal NaN 22
3 Divy 5.9 33
4 Anjali NaN 22

Rows where height is NaN:


name height age
2 Kamal NaN 22
4 Anjali NaN 22

Q6) Write a Python program to convert a dictionary to a Pandas


series.
import pandas as pd

d = {'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 800}
s = [Link](d)
print("Dictionary converted to Series:")
print(s)

Output:

Dictionary converted to Series:


a 100
b 200
c 300
d 400
e 800
dtype: int64
Q7) WAP to create a DataFrame of students which consist of 5
student name, class, five subject marks and grade of each student.
Display the name of the students. Calculate the sum of all the subject
marks of any one student.
import pandas as pd

data = {
'Name': ['Amit', 'Priya', 'Rohit', 'Sneha', 'Vikash'],
'Class': [12, 12, 12, 12, 12],
'Maths': [90, 78, 85, 60, 77],
'Physics': [92, 70, 80, 75, 66],
'Chemistry': [88, 72, 90, 66, 80],
'English': [85, 88, 86, 80, 84],
'CS': [80, 90, 81, 88, 79],
'Grade': ['A', 'B', 'A', 'C', 'B']
}
df = [Link](data)
print("Student DataFrame:")
print(df)
print("\nNames of students:")
print(df['Name'])
print("\nSum of all subject marks for Amit:")
sum_marks = df[df['Name']=='Amit'][['Maths','Physics','Chemistry','English','CS']].sum(axis=1)
print(sum_marks.values[0])

Output:

Student DataFrame:
Name Class Maths Physics Chemistry English CS Grade
0 Amit 12 90 92 88 85 80 A
1 Priya 12 78 70 72 88 90 B
2 Rohit 12 85 80 90 86 81 A
3 Sneha 12 60 75 66 80 88 C
4 Vikash 12 77 66 80 84 79 B

Names of students:
0 Amit
1 Priya
2 Rohit
3 Sneha
4 Vikash
Name: Name, dtype: object
Sum of all subject marks for Amit:
435

Q8) WAP to create a DataFrame for examination result and display


row labels, column labels, data types of each column and the
dimensions.
import pandas as pd

data = {
'Name': ['Amit', 'Priya', 'Rohit'],
'Maths': [90, 80, 70],
'English': [85, 75, 65],
'Science': [88, 78, 68]
}
df = [Link](data)
print("Examination Result DataFrame:")
print(df)
print("\nRow labels (index):")
print([Link])
print("\nColumn labels:")
print([Link])
print("\nData Types:")
print([Link])
print("\nDimensions (shape):")
print([Link])

Output:

Examination Result DataFrame:


Name Maths English Science
0 Amit 90 85 88
1 Priya 80 75 78
2 Rohit 70 65 68

Row labels (index):


RangeIndex(start=0, stop=3, step=1)

Column labels:
Index(['Name', 'Maths', 'English', 'Science'], dtype='object')

Data Types:
Name object
Maths int64
English int64
Science int64
dtype: object

Dimensions (shape):
(3, 4)

Q9) WAP to create a DataFrame to store weight, age and name of 5


people. Print the number of rows, row index, column index and
transpose of the above created DataFrame.
import pandas as pd

df = [Link]({
'Name': ['Amit', 'Priya', 'Rohit', 'Sneha', 'Vikash'],
'Age': [21, 22, 20, 23, 24],
'Weight': [55, 60, 58, 63, 50]
})
print("DataFrame:")
print(df)
print("\nNumber of Rows:", [Link][0])
print("\nRow Index:")
print([Link])
print("\nColumn Index:")
print([Link])
print("\nTranspose:")
print(df.T)

Output:

DataFrame:
Name Age Weight
0 Amit 21 55
1 Priya 22 60
2 Rohit 20 58
3 Sneha 23 63
4 Vikash 24 50

Number of Rows: 5

Row Index:
RangeIndex(start=0, stop=5, step=1)

Column Index:
Index(['Name', 'Age', 'Weight'], dtype='object')

Transpose:
0 1 2 3 4
Name Amit Priya Rohit Sneha Vikash
Age 21 22 20 23 24
Weight 55 60 58 63 50

Q10) Create a dataframe with fruits, their quantity and their price,
calculate the total amount paid by user and display details about any
particular fruit and the maximum amount which a user is paying for
which fruit.
import pandas as pd

df = [Link]({
'Fruit': ['Apple', 'Banana', 'Orange', 'Mango'],
'Quantity': [2, 6, 4, 3],
'Price': [50, 10, 20, 30]
})
df['Total'] = df['Quantity'] * df['Price']
print("Fruits DataFrame:")
print(df)
print("\nDetails of Apple:")
print(df[df['Fruit']=='Apple'])
print("\nFruit with maximum amount paid:")
max_fruit = [Link][df['Total'].idxmax()]
print(max_fruit)

Output:
Fruits DataFrame:
Fruit Quantity Price Total
0 Apple 2 50 100
1 Banana 6 10 60
2 Orange 4 20 80
3 Mango 3 30 90

Details of Apple:
Fruit Quantity Price Total
0 Apple 2 50 100

Fruit with maximum amount paid:


Fruit Apple
Quantity 2
Price 50
Total 100
Name: 0, dtype: object

Q11) Create 2 dataframes of marks of five subjects of students and


add them. Show the total marks of the students.
import pandas as pd

df1 = [Link]({
'Maths': [90, 80, 70],
'English': [85, 75, 65],
'Science': [88, 78, 68],
'History': [82, 72, 62],
'Geography': [86, 76, 66]
})

df2 = [Link]({
'Maths': [10, 20, 30],
'English': [15, 25, 35],
'Science': [12, 22, 32],
'History': [18, 28, 38],
'Geography': [14, 24, 34]
})

total = df1 + df2


print("DataFrame 1:")
print(df1)
print("\nDataFrame 2:")
print(df2)
print("\nTotal marks (df1 + df2):")
print(total)

Output:

DataFrame 1:
Maths English Science History Geography
0 90 85 88 82 86
1 80 75 78 72 76
2 70 65 68 62 66

DataFrame 2:
Maths English Science History Geography
0 10 15 12 18 14
1 20 25 22 28 24
2 30 35 32 38 34

Total marks (df1 + df2):


Maths English Science History Geography
0 100 100 100 100 100
1 100 100 100 100 100
2 100 100 100 100 100

Q12) WAP to replace all negative values in a dataframe with a 0.


import pandas as pd

df = [Link]({
'A': [1, -2, 3, -4],
'B': [-1, 2, -3, 4],
'C': [5, -6, 7, -8]
})
print("Original DataFrame:")
print(df)
df[df < 0] = 0
print("\nDataFrame after replacing negative values with 0:")
print(df)
Output:

Original DataFrame:
A B C
0 1 -1 5
1 -2 2 -6
2 3 -3 7
3 -4 4 -8

DataFrame after replacing negative values with 0:


A B C
0 1 0 5
1 0 2 0
2 3 0 7
3 0 4 0

Q13) Write a program that stores the sales of 5 fast moving items of
a store for each month in 12 Series objects.
import pandas as pd

# Creating 12 Series objects for 12 months


series_list = []
for month in range(1, 13):
sales = [Link]([100+month*5, 200+month*3, 150+month*4, 300+month*2, 250+month*6])
series_list.append(sales)

print("Sales for Month 1:")


print(series_list[0])
print("\nSales for Month 6:")
print(series_list[5])
print("\nSales for Month 12:")
print(series_list[11])

Output:

Sales for Month 1:


0 105
1 203
2 154
3 302
4 256
dtype: int64

Sales for Month 6:


0 130
1 218
2 174
3 312
4 286
dtype: int64

Sales for Month 12:


0 160
1 236
2 198
3 324
4 322
dtype: int64

Q14) WAP to create a dataframe prodf which will be having the


production of rice, pulses, fruits and wheat for 5 different states.
import pandas as pd

df = [Link]({
'State': ['Maharashtra', 'Punjab', 'Uttar Pradesh', 'West Bengal', 'Tamil Nadu'],
'Rice': [100, 200, 150, 180, 170],
'Pulses': [50, 70, 65, 60, 55],
'Fruits': [90, 85, 80, 95, 88],
'Wheat': [120, 130, 125, 140, 110]
})
print("Production DataFrame:")
print(df)

Output:

Production DataFrame:
State Rice Pulses Fruits Wheat
0 Maharashtra 100 50 90 120
1 Punjab 200 70 85 130
2 Uttar Pradesh 150 65 80 125
3 West Bengal 180 60 95 140
4 Tamil Nadu 170 55 88 110

Q15) Given the school result data, analyse the performance of the
students on different parameters, e.g Subject-wise or class-wise.
import pandas as pd

data = {
'Name': ['Amit', 'Priya', 'Rohit', 'Sneha', 'Vikash'],
'Class': [12, 11, 12, 11, 12],
'Maths': [90, 78, 85, 60, 77],
'English': [85, 88, 86, 80, 84],
'Science': [88, 75, 90, 78, 80]
}
df = [Link](data)
print("School Result Data:")
print(df)
print("\nSubject-wise Average:")
print(df[['Maths', 'English', 'Science']].mean())
print("\nClass-wise Analysis:")
print([Link]('Class').mean())

Output:

School Result Data:


Name Class Maths English Science
0 Amit 12 90 85 88
1 Priya 11 78 88 75
2 Rohit 12 85 86 90
3 Sneha 11 60 80 78
4 Vikash 12 77 84 80

Subject-wise Average:
Maths 78.0
English 84.6
Science 82.2
dtype: float64
Class-wise Analysis:
Maths English Science
Class
11 69.0 84.0 76.5
12 84.0 85.0 86.0

Q16) WAP to create scatter chart to display the score of two students
in 5 subjects.
import pandas as pd
import [Link] as plt

data = {
'Subjects': ['Math', 'English', 'Science', 'CS', 'PE'],
'Student1': [87, 93, 77, 88, 92],
'Student2': [78, 85, 89, 90, 80]
}
df = [Link](data)
[Link](df['Subjects'], df['Student1'], label='Student1', color='blue')
[Link](df['Subjects'], df['Student2'], label='Student2', color='red')
[Link]('Subjects')
[Link]('Marks')
[Link]('Score of Two Students in 5 Subjects')
[Link]()
[Link]()

Output:

Q17) WAP to create Line charts for plotting Year wise Sales of TV
Brands A, B, C, D.
import [Link] as plt

years = [2020, 2021, 2022, 2023]


brand_a = [100, 150, 130, 180]
brand_b = [90, 120, 110, 140]
brand_c = [110, 140, 120, 160]
brand_d = [95, 110, 105, 130]

[Link](years, brand_a, label='Brand A', marker='o')


[Link](years, brand_b, label='Brand B', marker='s')
[Link](years, brand_c, label='Brand C', marker='^')
[Link](years, brand_d, label='Brand D', marker='*')
[Link]('Year')
[Link]('Sales')
[Link]('Year wise Sales of TV Brands A, B, C, D')
[Link]()
[Link](True)
[Link]()

Output:

Q18) WAP to plot a Bar chart for the sales of TV of brand A in last
four years.
import [Link] as plt

years = [2020, 2021, 2022, 2023]


sales = [100, 150, 130, 180]
[Link](years, sales, color='skyblue')
[Link]('Year')
[Link]('Sales of TV Brand A')
[Link]('Sales of TV Brand A in Last Four Years')
[Link](axis='y')
[Link]()

Output:

Q19) Consider the School is collecting money for charity. Write a


program to plot the amount collected vs. days using a bar chart.
import [Link] as plt

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']


amount = [1000, 2300, 3100, 720, 1600]
[Link](days, amount, color='green')
[Link]('Days')
[Link]('Amount Collected (Rs.)')
[Link]('Charity Amount Collected vs Days')
[Link](rotation=45)
[Link](axis='y')
[Link]()

Output:
Q20) Given DataFrame, plot line and bar charts.
import pandas as pd
import [Link] as plt

df = [Link]({
1990: [54, 64, 79, 96],
2000: [345, 485, 690, 770],
2010: [895, 562, 1100, 890]
})
print("DataFrame:")
print(df)

# Line chart for 1990 and 2000


[Link](figsize=(10, 5))
[Link](1, 2, 1)
df[[1990, 2000]].plot(kind='line', marker='o')
[Link]('Line Chart for 1990 and 2000')
[Link]('Row Index')
[Link]('Values')

# Bar chart for all columns


[Link](1, 2, 2)
[Link](kind='bar')
[Link]('Bar Chart of All Columns')
[Link]('Row Index')
[Link]('Values')
[Link](rotation=0)

plt.tight_layout()
[Link]()

Output:

DataFrame:
1990 2000 2010
0 54 345 895
1 64 485 562
2 79 690 1100
3 96 770 890
Q21) WAP to import data between pandas and CSV file.
import pandas as pd

# Create sample data


data = {
'Name': ['Amit', 'Priya', 'Rohit'],
'Age': [21, 22, 20],
'City': ['Delhi', 'Mumbai', 'Bangalore']
}
df = [Link](data)

# Save to CSV
df.to_csv('sample_data.csv', index=False)
print("Data saved to CSV file")

# Read from CSV


df_imported = pd.read_csv('sample_data.csv')
print("Data imported from CSV:")
print(df_imported)

Output:

Data saved to CSV file


Data imported from CSV:
Name Age City
0 Amit 21 Delhi
1 Priya 22 Mumbai
2 Rohit 20 Bangalore
Q22) WAP to export data from pandas to CSV file.
import pandas as pd

df = [Link]({
'Product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor'],
'Price': [50000, 500, 1500, 15000],
'Quantity': [10, 50, 30, 15]
})

print("DataFrame to export:")
print(df)

# Export to CSV
df.to_csv('[Link]', index=False)
print("\nData exported to '[Link]' successfully!")

Output:

DataFrame to export:
Product Price Quantity
0 Laptop 50000 10
1 Mouse 500 50
2 Keyboard 1500 30
3 Monitor 15000 15

Data exported to '[Link]' successfully!

23. Histogram for Student Marks


import pandas as pd
import [Link] as plt

data = {
'Name': ['Amit', 'Priya', 'Rohit', 'Sneha', 'Vikash'],
'Maths': [87, 78, 93, 90, 85],
'Physics': [92, 80, 81, 88, 79],
'Chemistry': [88, 75, 90, 78, 84]
}
df = [Link](data)
[Link](df['Maths'], bins=5, edgecolor='black', color='skyblue')
[Link]('Marks Range')
[Link]('Number of Students')
[Link]('Histogram of Maths Marks Distribution')
[Link](axis='y', alpha=0.3)
[Link]()

Output:

You might also like