Consider the following DataFrame named "marks":
Arnab Sumit Sonam Maths 84 78 85 Science 86 84 86 Hindi 78 95 94
Which of the following option is correct to insert the record of another student named "Priya"?
Consider the following DataFrame named "marks":
| Arnab | Sumit | Sonam | |
|---|---|---|---|
| Maths | 84 | 78 | 85 |
| Science | 86 | 84 | 86 |
| Hindi | 78 | 95 | 94 |
Which of the following option is correct to insert the record of another student named "Priya"?
Solution
In a pandas DataFrame, students are columns, so a new student is added as a new column. The syntax is df["column_name"] = values, which here is marks["Priya"]=[78,89,76]. The list has three values, one for each row (Maths, Science, Hindi).
Option 1: this reassigns the variable marks to a plain list and destroys the DataFrame.
Option 2: marks[2] uses an integer as the column label. This creates a column named 2, not "Priya", or raises an error.
Option 3: Priya without quotes is treated as a variable name, which is not defined, so it raises a NameError.
Related questions:
2026: 23 May Shift 1
2025: 13 May Shift 2