0% found this document useful (0 votes)
103 views25 pages

XII IP Practical List - Anand

Uploaded by

Andy
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)
103 views25 pages

XII IP Practical List - Anand

Uploaded by

Andy
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
You are on page 1/ 25

XII IP practical Program List

1. Create the following data frame using a dictionary.

1) #code
import pandas as pd

datadict = {'Name': ['Tanmay', 'Krish', 'Aditi', 'Mehak', 'Anjali', 'Kriti', 'Niti'],


'class': ['XI A', 'XI B', 'XI B', 'XI A', 'XI A', 'XI A', 'XI B'],
'marks': [95, 87, 82, 65, 77, 45, 90]}
data = pd.DataFrame(datadict)
print(data, data.loc[:, ['marks']].min(), data.loc[:, ['marks']].max(), sep='\n\n')

Output:

marks 45
dtype: int64

marks 95
dtype: int64

2. Write a program to create a data frame salesman using the series sales_person which


stored salesman names and quantity of sales of the previous month. (1) Iterate the data
frame created by its columns. (2) Print sales of salesman along with their index using
iteritems

2) #code
import pandas as pd

sales_person = pd.Series(['Ham', 'Burr', 'George', 'Seabury'], [43, 38, 53, 47])


salesman = pd.DataFrame({'Name': sales_person.values, 'Sales': sales_person.index})

for i, j in salesman.iteritems():
print(j)
Output:

0 Ham
1 Burr
2 George
3 Seabury
Name: Name, dtype: object
0 43
1 38
2 53
3 47
Name: Sales, dtype: int64

3. Write a program to create a data frame of five countries using a dictionary which


stores the country names, capitals and populations of the country.

3) #code
import pandas as pd

cont_dict = {'Name': ['India', 'Japan', 'Russia', 'England', 'Spain'],


'Capital': ['New Delhi', 'Tokyo', 'Moscow', 'London', 'Madrid'],
'Population': [1393409038, 125651220, 145824556, 59597300, 46793385]}
cont_df = pd.DataFrame(cont_dict)
print(cont_df)

Output:

Name Capital Population


0 India New Delhi 1393409038
1 Japan Tokyo 125651220
2 Russia Moscow 145824556
3 England London 59597300
4 Spain Madrid 46793385

4. Create the following data frame,


1. Print the batsman's name along with runs scored in Test and T20 using column
names
2. Display the Batsman name along with runs scored in ODI using loc.
Display the batsman details who scored runs more than :
1. More than 2000 in ODI
2. Less than 2500 in Test
3. More than 1500 in T20

3. Display the columns using column index numbers like 0, 2, and 4.


4. Insert 2 rows in the data frame and delete rows whose index is 1 and 4.
5. Delete columns T20 and total using columns parameter in drop() function.
6. Display the first two rows and the last two rows.
7. Count the total number of rows and columns of the data frame.

4) #code
import pandas as pd

cricket_df = pd.DataFrame({'S.No.': [1, 2, 3, 4, 5],


'Batsman': ['Virat Kohli', 'Ajinkya Rehane', 'Rohit Sharma', 'Shikhar
Dhawan',
'Hardik Pandya'],
'Test': [3543, 2578, 2280, 2158, 1879], 'ODI': [2245, 2165, 2080, 1957,
1856],
'T20': [1925, 1853, 1522, 1020, 980]})
print(cricket_df, cricket_df[['Batsman', 'Test', 'T20']], cricket_df.loc[:, ['Batsman', 'ODI']],
cricket_df[(cricket_df['ODI'] > 2000)], cricket_df[(cricket_df['Test'] < 2500)],
cricket_df[(cricket_df['T20'] > 1500)], cricket_df.iloc[:, ::2], sep='\n\n', end='\n\n')
cricket_df.loc[5], cricket_df.loc[6] = [6, 'MS Dhoni', 3367, 2768, 2484], [7, 'Ravi Ashwin',
2564, 1875, 2242]
cricket_df.drop([1, 4], inplace=True)
print(cricket_df, '\n\n')
cricket_df.drop(columns='T20', inplace=True)
print(cricket_df, cricket_df.head(2), cricket_df.tail(2), len(cricket_df.index),
len(cricket_df.columns), sep='\n\n',
end='\n\n')

Output:

S.No. Batsman Test ODI T20


0 1 Virat Kohli 3543 2245 1925
1 2 Ajinkya Rehane 2578 2165 1853
2 3 Rohit Sharma 2280 2080 1522
3 4 Shikhar Dhawan 2158 1957 1020
4 5 Hardik Pandya 1879 1856 980

Batsman Test T20


0 Virat Kohli 3543 1925
1 Ajinkya Rehane 2578 1853
2 Rohit Sharma 2280 1522
3 Shikhar Dhawan 2158 1020
4 Hardik Pandya 1879 980

Batsman ODI
0 Virat Kohli 2245
1 Ajinkya Rehane 2165
2 Rohit Sharma 2080
3 Shikhar Dhawan 1957
4 Hardik Pandya 1856

S.No. Batsman Test ODI T20


0 1 Virat Kohli 3543 2245 1925
1 2 Ajinkya Rehane 2578 2165 1853
2 3 Rohit Sharma 2280 2080 1522

S.No. Batsman Test ODI T20


2 3 Rohit Sharma 2280 2080 1522
3 4 Shikhar Dhawan 2158 1957 1020
4 5 Hardik Pandya 1879 1856 980

S.No. Batsman Test ODI T20


0 1 Virat Kohli 3543 2245 1925
1 2 Ajinkya Rehane 2578 2165 1853
2 3 Rohit Sharma 2280 2080 1522

S.No. Test T20


0 1 3543 1925
1 2 2578 1853
2 3 2280 1522
3 4 2158 1020
4 5 1879 980

S.No. Batsman Test ODI T20


0 1 Virat Kohli 3543 2245 1925
2 3 Rohit Sharma 2280 2080 1522
3 4 Shikhar Dhawan 2158 1957 1020
5 6 MS Dhoni 3367 2768 2484
6 7 Ravi Ashwin 2564 1875 2242

S.No. Batsman Test ODI


0 1 Virat Kohli 3543 2245
2 3 Rohit Sharma 2280 2080
3 4 Shikhar Dhawan 2158 1957
5 6 MS Dhoni 3367 2768
6 7 Ravi Ashwin 2564 1875

S.No. Batsman Test ODI


0 1 Virat Kohli 3543 2245
2 3 Rohit Sharma 2280 2080

S.No. Batsman Test ODI


5 6 MS Dhoni 3367 2768
6 7 Ravi Ashwin 2564 1875

5. Write a Pandas program to convert a dictionary to a Pandas series.

Sample dictionary: d1 = {'a': 100, 'b': 200, 'c':300, 'd':400, 'e':800}

5) #code

import pandas as pd
d1 = {'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 800}

s1 = pd.Series(d1)

print(s1)

Output:

a 100

b 200

c 300

d 400

e 800

dtype: int64

6. Consider a given Series, Subject:

Write a program in Python Pandas to create this series.

6) #code
import pandas as pd

Subject = pd.Series([75, 78, 82, 86], index=['ENGLISH', 'HINDI', 'MATHS', 'SCIENCE'])


print(Subject)

Output:

ENGLISH 75
HINDI 78
MATHS 82
SCIENCE 86
dtype: int64

7. Consider a given Series, M1:

Write a program in Python Pandas to create the series.

7) #code
import pandas as pd

M1 = pd.Series([45, 65, 24, 89], index=['Term1', 'Term2', 'Term3', 'Term4'])


print(M1)

Output:

Term1 45
Term2 65
Term3 24
Term4 89
dtype: int64

8. Write a program in Python Pandas to create the following DataFrame batsman from a
Dictionary:

Perform the following operations on the DataFrame: 1)Add both the scores of a
batsman and assign them to column “Total” 2)Display the highest score in both
Score1 and Score2 of the DataFrame.

8) #code
import pandas as pd

batsman_dict = {'B_NO': [1, 2, 3, 4], 'Name': ['Sunil Pillai', 'Gaurav Sharma', 'Piyush Goel',
'Kartik Thakur'],
'Score1': [90, 65, 70, 80], 'Score2': [80, 45, 90, 76]}
batsman = pd.DataFrame(batsman_dict)
print(batsman, end='\n\n')
batsman['Total'] = batsman['Score1'] + batsman['Score2']
print(batsman, batsman[['Score1', 'Score2']].max(), sep='\n\n')

Output:

B_NO Name Score1 Score2


0 1 Sunil Pillai 90 80
1 2 Gaurav Sharma 65 45
2 3 Piyush Goel 70 90
3 4 Kartik Thakur 80 76

B_NO Name Score1 Score2 Total


0 1 Sunil Pillai 90 80 170
1 2 Gaurav Sharma 65 45 110
2 3 Piyush Goel 70 90 160
3 4 Kartik Thakur 80 76 156

Score1 90
Score2 90
dtype: int64

9. Write a program in Python Pandas to create the following DataFrame


toppers from a Dictionary:

Perform the following operations on the DataFrame :


1)Add both the marks from PB1 and PB2 of a student and assign them to a column
“Final”
2)Display the highest marks in both PB1 and PB2 of the DataFrame.
3)Display the DataFrame

9) #code
import pandas as pd

toppers_dict = {'T_NO': [1, 2, 3, 4], 'Name': ['Pavan', 'Sugandha', 'Pulkita', 'Sahil'],


'PB1': [90, 85, 70, 69], 'PB2': [80, 75, 72, 71]}
toppers = pd.DataFrame(toppers_dict)
print(toppers, end='\n\n')
toppers['Final'] = toppers['PB1'] + toppers['PB2']
print(toppers, toppers[['PB1', 'PB2']].max(), sep='\n\n')

Output:

T_NO Name PB1 PB2


0 1 Pavan 90 80
1 2 Sugandha 85 75
2 3 Pulkita 70 72
3 4 Sahil 69 71

T_NO Name PB1 PB2 Final


0 1 Pavan 90 80 170
1 2 Sugandha 85 75 160
2 3 Pulkita 70 72 142
3 4 Sahil 69 71 140

PB1 90
PB2 80
dtype: int64

10. Write a code to create the following data frame.

Write commands to :
i. Add a new column ‘Winner’ to the Dataframe
ii. Add a new row with values ( s5, 5, Handball, 20)

10) #code
import pandas as pd

sports = pd.DataFrame({'Sports_Id': [1, 2, 3, 4], 'Name': ['Hockey', 'Cricket', 'Chess',


'Carrom'],
'Student': [15, 20, 4, 4], 'Team': ['B', 'D', 'C', 'A']}, index=['s1', 's2', 's3', 's4'])
print(sports, end='\n\n')
sports['Winner'] = ['AA', 'BB', 'CC', 'DD']
sports.loc[4] = ([5, 'Handball', 20, 'E', 'EE'])
sports.rename(index={4: 's5'}, inplace=True)
print(sports)

Output:

Sports_Id Name Student Team


s1 1 Hockey 15 B
s2 2 Cricket 20 D
s3 3 Chess 4 C
s4 4 Carrom 4 A

Sports_Id Name Student Team Winner


s1 1 Hockey 15 B AA
s2 2 Cricket 20 D BB
s3 3 Chess 4 C CC
s4 4 Carrom 4 A DD
s5 5 Handball 20 E EE

11. Write a Python code to create the following DataFrame Library using Python Pandas.
Give index as ‘B1’, ‘B2’, ‘B3’, ‘B4’

(i) Display Item Number and Name whose price is less than 150.
(ii) Display the top two and bottom two rows.
(iii) Display the DataFrame.

11) #code
import pandas as pd

df = pd.DataFrame({'ItemNo': ['I99', 'I10', 'I50', 'I60'], 'ItemName': ['Sugar', 'Tea', 'Coffee',


'Green Tea'],
'Price': [100, 150, 200, 250]}, index=['B1', 'B2', 'B3', 'B4'])
print(df, df[(df['Price'] < 150)][['ItemNo', 'ItemName']], df.head(2), df.tail(2), sep='\n\n',
end='\n\n')

Output:
ItemNo ItemName Price
B1 I99 Sugar 100
B2 I10 Tea 150
B3 I50 Coffee 200
B4 I60 Green Tea 250

ItemNo ItemName
B1 I99 Sugar

ItemNo ItemName Price


B1 I99 Sugar 100
B2 I10 Tea 150

ItemNo ItemName Price


B3 I50 Coffee 200
B4 I60 Green Tea 250

12. The number of bed sheets manufactured by a factory during five consecutive weeks is
given below.

Draw the bar graph representing the above data


1. Add a title to the graph.

12) #code
import matplotlib.pyplot as plt

week = ['First', 'Second', 'Third', 'Fourth', 'Fifth']


no_sheets = [600, 850, 700, 300, 900]
plt.bar(week, no_sheets, color=['g', 'r', 'b', 'y', 'c'])
plt.title('Bed Sheet Manufacturing')
plt.xlabel('Weeks')
plt.ylabel('Number of Bed-Sheets')
plt.show()

Output:
13. The number of students in 7 different classes is given below. Represent this data on
the bar graph.

13) #code
import matplotlib.pyplot as plt

grade = ['6th', '7th', '8th', '9th', '10th', '11th', '12th']


no_students = [130, 120, 135, 130, 150, 80, 75]
plt.bar(grade, no_students, color=['g', 'r', 'b', 'y', 'c', 'm', 'k'])
plt.title('Students per Class')
plt.xlabel('Class')
plt.ylabel('Number of Students')
plt.show()
Output:

14. Consider the following graph. Write the code to plot it.

14) #code
import matplotlib.pyplot as plt
a = ['1', '2', '3', '4', '5']
b = [10, 31, 16, 12, 20]
plt.plot(a, b, marker='o', color='c')
plt.grid(axis='y')
plt.show()

Output:

15. Consider the following graph. Write the code to plot it.
15) #code
import matplotlib.pyplot as plt

a = ['VII', 'VIII', 'IX', 'X']


b = [40, 45, 35, 43]
plt.bar(a, b, color='c')
plt.show()

Output:
16. Write a Python program to display a bar chart of the number of students in a school.
Use different colours for each bar.
Sample data: Class: I,II,III,IV,V,VI,VII,VIII,IX,X Strength:
38,30,45,49,37,53,48,44,36,46

16) #code
import matplotlib.pyplot as plt

a = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X']
b = [38, 30, 45, 49, 37, 53, 48, 44, 36, 46]
plt.bar(a, b, color=['g', 'r', 'b', 'y', 'c', 'm', 'k', 'olive', 'brown', 'silver'])
plt.title('Students per Class')
plt.xlabel('Class')
plt.ylabel('Number of Students')
plt.show()

Output:
17. Write a Python program to plot the given bar graph to depict the popularity of various
programming languages. Label the graph with x-axis, y-axis, y-ticks and title. Data :
Programming languages: Python, C++, Java, Perl, Scala, Lisp Usage= 10,8,6,4,2,1

17) #code
import matplotlib.pyplot as plt

a = ['Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp']


b = [10, 8, 6, 4, 2, 1]
plt.barh(a, b, color='c')
plt.title('Programming Languages Popularity')
plt.xlabel('Programming Languages')
plt.ylabel('Usage')
plt.yticks([0, 2, 4, 6, 8, 10])
plt.show()

Output:

18. Consider the following data of a medical store and plot the data on the line chart:
18) #code
import matplotlib.pyplot as plt

month = ['March', 'April', 'May', 'June', 'July', 'August']


masks = [1500, 3500, 6500, 6700, 6000, 6800]
sanitizer = [4400, 4500, 5500, 6000, 5600, 6300]
handwash = [6500, 5000, 5800, 6300, 6200, 4500]
plt.plot(month, masks, label='Masks')
plt.plot(month, sanitizer, label='Sanitizer')
plt.plot(month, handwash, label='Handwash')
plt.legend(loc='lower right')
plt.title('Medical Store')
plt.show()

Output:
19.  Plot following data on line chart:

1. Write a title for the chart “The Weekly Income Report”.


2. Write the appropriate titles of both axes.
3. Write code to Display legends.
4. Display red colour for the line.
5. Use the line style – dashed
6. Display diamond-style markers on data points

19) #code
import matplotlib.pyplot as plt

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


income = [510, 350, 475, 580, 600]
plt.plot(day, income, label='Income', color='r', linestyle='--', marker='D')
plt.xlabel('Day')
plt.ylabel('Income')
plt.legend(loc='lower right')
plt.title('The Weekly Income Report')
plt.show()

Output:

20. Observe the following data and plot data according to the given instructions:
1. Create a bar chart to display data of Virat Kohli & Rohit Sharma.
● Customise the chart in this manner
● Use different widths
● Use different colours to represent different years' score
● Display appropriate titles for axis and chart
● Show legends

20) #code
import matplotlib.pyplot as plt
import numpy as np

barWidth = 0.25
batsmen = ['Virat Kohli', 'Steve Smith', 'Babar Azam', 'Rohit Sharma', 'Kane Williamson', 'Jos
Butler']
y2017 = [2501, 2340, 1750, 1463, 1256, 1125]
y2018 = [1855, 2250, 2147, 1985, 1785, 1853]
y2019 = [2203, 2003, 1896, 1854, 1874, 1769]
y2020 = [1223, 1153, 1008, 1638, 1974, 1436]
Virat_Kohli = [2501, 1855, 2203, 1223]
Rohit_Sharma = [1463, 1985, 1854, 1638]
years = np.arange(2017, 2021)

plt.bar(years, Virat_Kohli, width=0.25, label='Virat Kohli', color='c')


plt.bar(years + 0.25, Rohit_Sharma, width=0.2, label='Rohit Sharma', color='r')
plt.xticks([2017, 2018, 2019, 2020])
plt.xlabel('Years')
plt.ylabel('Score')
plt.legend(loc='upper right')
plt.title('Batsmen Data')
plt.show()

Output:

21. Draw the histogram based on the Production of Wheat in different Years
Year:2000,2002,2004,2006,2008,2010,2012,2014,2016,2018
Production':4,6,7,15,24,2,19,5,16,4

21) #code
import matplotlib.pyplot as plt
prod = [4, 6, 7, 15, 24, 2, 19, 5, 16, 4]
plt.hist(prod, bins=20)
plt.title('Production')
plt.show()

Output:

You might also like