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.
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.
3Modifying a single cell
To change just one value, target the exact cell with .at (labels) or .iat (positions). Row first, then column.
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.df has no 'Science' column. What does df['Science'] = 50 do?
Which changes just ONE cell — Pankaj's IP mark?
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)