Python Pandas — I — Question 7
Back to all questionsConsider the following DataFrame df and answer any four questions from (i)-(v):
| rollno | name | UT1 | UT2 | UT3 | UT4 |
|---|---|---|---|---|---|
| 1 | Prerna Singh | 24 | 24 | 20 | 22 |
| 2 | Manish Arora | 18 | 17 | 19 | 22 |
| 3 | Tanish Goel | 20 | 22 | 18 | 24 |
| 4 | Falguni Jain | 22 | 20 | 24 | 20 |
| 5 | Kanika Bhatnagar | 15 | 20 | 18 | 22 |
| 6 | Ramandeep Kaur | 20 | 15 | 22 | 24 |
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)
df1 = df[df.rollno == 4] print(df1)
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.