Informatics Practices

Consider the following DataFrame df and answer any four questions from (i)-(v):

rollnonameUT1UT2UT3UT4
1Prerna Singh24242022
2Manish Arora18171922
3Tanish Goel20221824
4Falguni Jain22202420
5Kanika Bhatnagar15201822
6Ramandeep Kaur20152224

The teacher needs to know the marks scored by the student with roll number 4. Help her identify the correct set of statement/s from the given options:

(a) df1 = df[df['rollno'] == 4]
print(df1)

(b) df1 = df[rollno == 4]
print(df1)

(c) df1 = df.[df.rollno = 4]
print(df1)

(d) df1 = df[df.rollno == 4]
print(df1)

Python Pandas

2 Likes

Answer

df1 = df[df.rollno == 4] print(df1)

Explanation

The statement df1 = df[df.rollno == 4] filters the DataFrame df to include only the rows where the roll number is equal to 4. This is accomplished using boolean indexing, where a boolean mask is created by checking if each row's rollno is equal to 4. Rows that satisfy this condition (True in the boolean mask) are selected, while others are excluded. The resulting DataFrame df1 contains only the rows corresponding to roll number 4 from the original DataFrame df.

Answered By

1 Like


Related Questions