Informatics Practices
Write a program in Python Pandas to create the following DataFrame batsman from a Dictionary :
| B_NO | Name | Score1 | Score2 |
|---|---|---|---|
| 1 | Sunil Pillai | 90 | 80 |
| 2 | Gaurav Sharma | 65 | 45 |
| 3 | Piyush Goel | 70 | 90 |
| 4 | Karthik Thakur | 80 | 76 |
Perform the following operations on the DataFrame :
(i) Add both the scores of a batsman and assign to column "Total".
(ii) Display the highest score in both Score1 and Score2 of the DataFrame.
(iii) Display the DataFrame.
Python Pandas
7 Likes
Answer
import pandas as pd
data = {'B_NO': [1, 2, 3, 4], 'Name': ['Sunil Pillai', 'Gaurav Sharma', 'Piyush Goel', 'Karthik Thakur'], 'Score1': [90, 65, 70, 80], 'Score2': [80, 45, 90, 76]}
batsman = pd.DataFrame(data)
batsman['Total'] = batsman['Score1'] + batsman['Score2']
highest_score1 = batsman['Score1'].max()
highest_score2 = batsman['Score2'].max()
print("Highest score in Score1: ", highest_score1)
print("Highest score in Score2: ", highest_score2)
print(batsman)
Output
Highest score in Score1: 90
Highest score in Score2: 90
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 Karthik Thakur 80 76 156
Answered By
3 Likes
Related Questions
What is the output of the following code ?
data = {'age': [20, 23, 22], 'name': ['Ruhi', 'Ali', 'Sam']} df1 = pd.DataFrame(data, index=[1, 2, 3]) print("Before") print(df1) df1['Edu'] = ['BA', 'BE' , 'MBA'] print('After') print(dfl)Consider the given DataFrame 'Genre' :
No Type Code 0 Fiction F 1 Non-fiction NF 2 Drama D 3 Poetry P Write suitable Python statements for the following :
(i) Add a column called Num_Copies with the following data : [300, 290, 450, 760].
(ii) Add a new genre of type 'Folk Tale' having code as "FT" and 600 number of copies.
(iii) Rename the column 'Code' to 'Book_Code'.
Consider the following dataframe, and answer the questions given below:
import pandas as pd df = pd.DataFrame( { "Quarter1": [2000, 4000, 5000, 4400, 10000], "Quarter2": [5800, 2500, 5400, 3000, 2900], "Quarter3": [20000, 16000, 7000, 3600, 8200], "Quarter4": [1400, 3700, 1700, 2000, 6000]})(i) Write the code to find mean value from above dataframe df over the index and column axis.
(ii) Use sum() function to find the sum of all the values over the index axis.
Write the use of the rename(mapper = <dict-like>, axis = 1) method for a Pandas Dataframe. Can the mapper and columns parameter be used together in a rename() method ?