0% found this document useful (0 votes)
2 views7 pages

Ssce Ai Practicals Python Answer Key

The document contains multiple sets of Python code examples demonstrating how to create and manipulate Pandas DataFrames and Series. It includes tasks such as selecting rows based on conditions, adding and updating rows and columns, and displaying data. Each set provides specific instructions and corresponding source code for achieving the tasks.
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)
2 views7 pages

Ssce Ai Practicals Python Answer Key

The document contains multiple sets of Python code examples demonstrating how to create and manipulate Pandas DataFrames and Series. It includes tasks such as selecting rows based on conditions, adding and updating rows and columns, and displaying data. Each set provides specific instructions and corresponding source code for achieving the tasks.
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

ANSWER KEY

SET – A :
1. Create a DataFrame students with the following data:
i) From the students DataFrame, select rows
SUBJECT RAM RAJ REKHA RIA
where Marks ≥ 90
MATHS 90 92 89 92
ii) Add a new row to a dataframe as English
SCIENCE 91 81 94 71
where values are (92,89,70,65)
HINDI 97 96 88 99
iii) Change the marks of science as 89,78,68,54
iv) Identify the head of the dataframe and the shape of the dataframe
v) Add a column Ravi and give the values as 40,25,88,97

SOURCE CODE:
import pandas as pd
import numpy as np

# Create the DataFrame


D = {'RAM': [90, 91, 97], 'RAJ': [92, 81, 96],\
'REKHA': [89, 94, 88], 'RIA': [92, 71, 99]}
students = [Link](D, index=['MATHS', 'SCIENCE', 'HINDI'])
print("Original DataFrame:")
print(students)

# i) Select rows where marks ≥ 90


print("\n Rows where marks ≥ 90:")
for row in [Link]:
for col in [Link]:
if [Link][row, col] >= 90:
print(row)
break

#IMPORTANT!!!
# What does [Link][row, column] return?
# It returns the individual value present at the specified row and
column of the DataFrame.

# ii) Add a new row 'ENGLISH'


[Link]['ENGLISH'] = [92, 89, 70, 65]
print("\nAfter adding ENGLISH row:")
print(students)
# iii) Change the marks of SCIENCE
[Link]['SCIENCE'] = [89, 78, 68, 54]
print("\nAfter updating SCIENCE marks:")
print(students)

# iv) Head and shape of the DataFrame


print("\nHead of the DataFrame:")
print([Link]())
print("\nShape of the DataFrame:")
print([Link])
# v) Add a new column 'RAVI'
students['RAVI'] = [40, 25, 88, 97]
print("\nAfter adding RAVI column:")
print(students)

SAMPLE OUTPUT:
SET – B :
1. Write python code to create a Pandas DataFrame using any sequence data type
a) Display the DataFrame
b) Display the first 5 records.
c) Display the last 10 records

SOURCE CODE:
import pandas as pd
# Creating sequence data (lists)
roll_no = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
maths = [65, 70, 72, 68, 75, 80, 85, 78, 74, 69, 90, 88, 92, 76, 81]
science = [60, 66, 70, 64, 71, 77, 83, 75, 72, 68, 88, 86, 90, 74, 79]

# Creating DataFrame using sequence data type:


df = [Link]({'Roll No': roll_no, 'Maths': maths, 'Science':
science})

# a) Display the DataFrame:


print("DataFrame:")
print(df)

# b) Display the first 5 records:


print("\nFirst 5 records:")
print([Link]())

# c) Display the last 10 records:


print("\nLast 10 records:")
print([Link](10))

SAMPLE OUTPUT :
SET – C :
1. Write a programe to create dataframe that stores the below value using three
different methods such as Numpy, dic onary, list of dic onary

SOURCE CODE:
import pandas as pd
import numpy as np

# Method 1: Using NumPy


Data = [Link]([[1, 85, 90], [2, 78, 88], [3, 92, 95]])
D = [Link](Data, columns=['Roll No', 'Maths', 'Science'])
print("DataFrame using NumPy:")
print(D)

# Method 2: Using Dictionary


Dict = {'Roll No': [1, 2, 3], 'Maths': [85, 78, 92], 'Science': [90,
88, 95]}
D_dict = [Link](Dict)

print("\nDataFrame using Dictionary:")


print(D_dict)

# Method 3: Using List of Dictionaries


Data_list_dict = [{'Roll No': 1, 'Maths': 85, 'Science': 90}, {
'Roll No': 2, 'Maths': 78, 'Science': 88}, {'Roll No': 3, 'Maths':
92, 'Science': 95}]
D_list_dict = [Link](Data_list_dict)
print("\nDataFrame using List of Dictionaries:")
print(D_list_dict)

SAMPLE OUTPUT :
SET – D :
1. Write Python code to create a Pandas Data Frame using any sequence data type (5 MARKS)

i) Display the Data Frame.


ii) Adding a New column to a DataFrame
iii) Adding a New Row to a DataFrame
iv) Deleting a New Row to a DataFrame
v) Deleting a New Row to a DataFrame

SOURCE CODE :
import pandas as pd

# Creating DataFrame
D = {'Maths': [78, 85, 90, 88]}
df = [Link](D, index=['A', 'B', 'C', 'D'])

# i) Display the DataFrame


print("DataFrame:")
print(df)

# ii) Adding a new column to the DataFrame


df['Science'] = [82, 79, 91, 87]
print("\nAfter adding new column (Science):")
print(df)

# iii) Adding a new row to the DataFrame


[Link]['E'] = [92, 89]
print("\nAfter adding new row:")
print(df)

# iv) Deleting a row from the DataFrame


df = [Link]('B')
print("\nAfter deleting a row:")
print(df)

# v) Deleting a column from the DataFrame


df = [Link]('Science', axis=1)
print("\nAfter deleting a column (Science):")
print(df)
SAMPLE OUTPUT :
SET – E :
1. Write a python program to create Series using pandas

SOURCE CODE :
import pandas as pd

# Creating a Series
D = [45, 67, 89, 72]
s = [Link](D)

print("Pandas Series:")
print(s)

SAMPLE OUTPUT:

You might also like