Sumit Jyoti Astha Rakesh Maths 90 85 68 90 Physics 92 87 75 87 Chemistry 91 79 71 71 Computer Science 88 76 84 88
A DataFrame 'df' is created as above. Which of the following syntax is the correct way for adding a row 'English' to this DataFrame?
| Sumit | Jyoti | Astha | Rakesh | |
|---|---|---|---|---|
| Maths | 90 | 85 | 68 | 90 |
| Physics | 92 | 87 | 75 | 87 |
| Chemistry | 91 | 79 | 71 | 71 |
| Computer Science | 88 | 76 | 84 | 88 |
A DataFrame 'df' is created as above. Which of the following syntax is the correct way for adding a row 'English' to this DataFrame?
Solution
df.loc['English'] = [67, 78, 89, 56] adds a new row. loc works with row labels, and assigning to a label that does not yet exist creates a new row with that label. The list has four values, matching the four columns (Sumit, Jyoti, Astha, Rakesh).
Options 2 and 4: iloc is purely position-based and accepts only integers, so a string label like 'English' raises an error. It also cannot create a new row.
Option 1: a DataFrame has no create attribute or method in pandas.
Remember: df.loc[label] = values adds a row, while df[label] = values adds a column.
Related questions:
2026: 21 May Shift 1
2026: 20 May Shift 1
2025: 30 May Shift 2
2026: 23 May Shift 1