Consider the DataFrame "Examdf" that has rows representing the subjects 'Maths', 'Science', and 'Hindi', and marks for six students in these subjects shown as follows:
Arnab Ramit Samridhi Riya Malika Preeti Maths 90 92 89 81 94 89 Science 91 81 78 67 99 76 Hindi 97 96 88 67 99 78
We need to add the marks [85, 86, 83, 80, 90, 89] for these six students in 'English' subject to this DataFrame. Which of the following is the correct code to implement this?
Consider the DataFrame "Examdf" that has rows representing the subjects 'Maths', 'Science', and 'Hindi', and marks for six students in these subjects shown as follows:
| Arnab | Ramit | Samridhi | Riya | Malika | Preeti | |
|---|---|---|---|---|---|---|
| Maths | 90 | 92 | 89 | 81 | 94 | 89 |
| Science | 91 | 81 | 78 | 67 | 99 | 76 |
| Hindi | 97 | 96 | 88 | 67 | 99 | 78 |
We need to add the marks [85, 86, 83, 80, 90, 89] for these six students in 'English' subject to this DataFrame. Which of the following is the correct code to implement this?
Solution
In Examdf, subjects are the row labels and students are the columns. English is a new subject, so it must be added as a new row with six values, one per student. Examdf.loc['English'] = [...] creates a new row labelled 'English' with these marks.
Option 2: Examdf['English'] = [...] adds a new column, not a row. It would also raise a length error, because the DataFrame has 3 rows but the list has 6 values.
Options 3 and 4: add is a method for arithmetic addition, and appendrow does not exist in pandas. Neither supports square-bracket assignment like this.
Remember: df['label'] = ... adds a column, and df.loc['label'] = ... adds a row.
Related questions:
2026: 13 May Shift 2
2026: 29 May Shift 2
2026: 24 May Shift 1
2025: 21 May Shift 1
2026: 29 May Shift 2