Write a Pandas program to select the rows where the number of attempts in the examination is greater than 2.

Program Logic:

  • Create Dictionary say ‘marks‘ which store marks of 5 subjects and total marks of students.
  • Create DataFrame say ‘result‘ using DataFrame method and print DataFrame.
  • Select Attempt column of data frame ‘result’ and use comparison operator>‘ on it
  • Display dataframe ‘result’ using print function
  • Exit

Below is implementation code/Source code:

Here is code for pandas program to select rows having number of attempt in examination is greater than 2.

import pandas as pd
marks  = { "English" :[67,89,90,55],
           "Maths":[55,67,45,56],
            "IP":[66,78,89,90],
           "Chemistry" :[45,56,67,65],
           "Biology":[54,65,76,87],
           "Total Marks":[287,355,367,353],"Percentage" :[57,71,73.4,70.6],
           "Attempts":[1,2,1,3]}

result = pd.DataFrame(marks,index=["Athang","Sujata","Sushil","Sumedh"])
print("********Marksheet************")
print(result)
print("Number of attempts in the examination is greater than 2:")
print(result[result['Attempts'] > 2])

Output:

Marksheet*
English Maths IP … Total Marks Percentage Attempts
Athang 67 55 66 … 287 57.0 1
Sujata 89 67 78 … 355 71.0 2
Sushil 90 45 89 … 367 73.4 1
Sumedh 55 56 90 … 353 70.6 3

[4 rows x 8 columns]
Number of attempts in the examination is greater than 2:
English Maths IP … Total Marks Percentage Attempts
Sumedh 55 56 90 … 353 70.6 3

[1 rows x 8 columns]

Below is Snapshot of executable code with output

Snapshot of code
Snapshot of output

You can also check out our other programs

Write a program to Count the number of rows and columns in given dataframe

Write a pandas program to select rows where score is between 15 and 20 (inclusive)

<

Leave a Reply

Your email address will not be published.