LambdaLabTM
Informatics Practices · Class 12 · Python Pandas · DataFrame
PandasDataFrame⏱️ 9 min read

Adding & Modifying

🎬 Watch the 6:45 video of this topic ↓

DataFrames are size-mutable — you can grow them. There's one rule that runs through all of it: assign to a name that exists and you modify it; assign to a new name and you create it.

1Adding & changing a column

Assign to df['col']. If col doesn't exist, a new column appears; if it does, its values change. A single value fills the whole column; a list sets each row.

df_columns.py
Watch Out
If you give a list, its length must match the number of rows — otherwise Pandas raises a ValueError.

2Adding & changing a row

Use .loc (or .at) with a row label and : for all columns. A missing label creates a new row; an existing one is overwritten.

df_rows.py

3Modifying a single cell

To change just one value, target the exact cell with .at (labels) or .iat (positions). Row first, then column.

df_cell.py
Watch Out
Don't change a cell with the column-then-row shortcut df['English']['ID 2'] = 50. Older Pandas changes the value, with a warning. Pandas 3 does not change the table at all. Use df.at['ID 2', 'English'] = 50 instead. The shortcut is still fine for reading one value.
Key Takeaway
The golden rule: assigning to an existing column/row modifies it; assigning to a new label creates it. A single value broadcasts to all cells; a list sets them one by one.
Quick Check

df has no 'Science' column. What does df['Science'] = 50 do?

Quick Check

Which changes just ONE cell — Pankaj's IP mark?

🎬 Watch the video

DataFrame: Add, Change, Delete, Rename, in 6:45

One animated video of this topic, narrated. Watch it before the lessons for the big picture, or after them to revise.

🔊 Hindi mein sunna hai? Watch on YouTube with Hindi audio ↗(there, choose ⚙ Settings → Audio track → Hindi)