Skip to main contentSkip to solution

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:

ArnabRamitSamridhiRiyaMalikaPreeti
Maths909289819489
Science918178679976
Hindi979688679978

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

✅ Correct Option: 1

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question