Informatics Practices · Class 12 · Python Pandas · DataFrame
PandasDataFrame⏱️ 8 min read
Deleting & Renaming
Real data is messy — you'll often need to throw out a column or row, or give a label a better name. drop() and rename() do exactly that, and both hinge on two arguments: axis and inplace.
1Deleting with drop()
drop() removes rows or columns. The axis argument decides which: axis=0 for a row (the default), axis=1 for a column.
df_drop.py
Note
By default
drop() returns a new DataFrame and leaves the original alone. Add inplace=True to change the original itself: df.drop('Hindi', axis=1, inplace=True).Tip
There's also the
del statement — del df['Hindi'] — but it can only delete a column, and it always changes the original.2Renaming with rename()
rename() changes labels using a dictionary of {old: new}. Use the index= argument for rows and columns= for columns — you can do both at once.
df_rename.py
Key Takeaway
drop() removes — axis=0 a row, axis=1 a column. rename() relabels — index= rows, columns= columns. Both need inplace=True to change the original DataFrame. Quick Check
How do you drop the column 'Hindi'?
Quick Check
After df.drop('ID 2', axis=0), why is the original df unchanged?