Informatics Practices · Class 12 · Python Pandas · DataFrame
PandasDataFrame⏱️ 9 min read
Adding & Modifying
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. .at (labels) and .iat (positions) are the clearest, but the df['col']['row'] shortcut works too.
df_cell.py
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?