drop() can remove rows by passing labels with the default axis=0, or the index parameter directly, or remove columns by passing axis=1, or the more explicit columns parameter, which avoids needing to remember which axis number means which. Like most pandas methods, it returns a new DataFrame and leaves the original unmodified unless you pass inplace=True or reassign the result back.
1Understanding df.drop()
drop() can remove rows by passing labels with the default axis=0, or the index parameter directly, or remove columns by passing axis=1, or the more explicit columns parameter, which avoids needing to remember which axis number means which. Like most pandas methods, it returns a new DataFrame and leaves the original unmodified unless you pass inplace=True or reassign the result back.
Use the explicit columns=[...] parameter instead of labels=[...] with axis=1 when dropping columns — it's clearer about intent and avoids needing to remember which axis number corresponds to columns.
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
print(df.drop(columns=["b"]))2Practical Example
Here is a real-world application of df.drop() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3]}, index=["x", "y", "z"])
print(df.drop(index=["y"]))3Best Practices
Follow these guidelines when working with df.drop():
1. Use the columns= parameter directly for dropping columns, rather than labels= combined with axis=1, for clarity
2. Remember drop() returns a new DataFrame by default — capture the result or pass inplace=True to actually modify the original
3. Pass errors='ignore' when dropping labels that might not exist in every case, to avoid a KeyError for labels that happen to already be missing
Tip: Use the explicit columns=[...] parameter instead of labels=[...] with axis=1 when dropping columns — it's clearer about intent and avoids needing to remember which axis number corresponds to columns.
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
print(df.drop(columns=["b"]))