columns is technically just the DataFrame's column-axis Index, the same Index type used for rows, which is why column names support the same fast, label-based lookup behavior as a row index. Reading df.columns gives you the current column names as an Index, convert to a plain list with .tolist() if needed, and assigning a new list of the same length directly to df.columns replaces every column name at once — a common way to rename columns in bulk when you have a full replacement list, as opposed to df.rename(), which is better suited for renaming just a few specific columns by their old name.
1Understanding df.columns
columns is technically just the DataFrame's column-axis Index, the same Index type used for rows, which is why column names support the same fast, label-based lookup behavior as a row index. Reading df.columns gives you the current column names as an Index, convert to a plain list with .tolist() if needed, and assigning a new list of the same length directly to df.columns replaces every column name at once — a common way to rename columns in bulk when you have a full replacement list, as opposed to df.rename(), which is better suited for renaming just a few specific columns by their old name.
Assigning a whole new list to df.columns replaces every column name at once and requires the new list to have exactly the same length as the current number of columns — use df.rename(columns={...}) instead when you only want to rename a few specific columns by name.
import pandas as pd
df = pd.DataFrame({"first_name": ["Alice"], "last_name": ["Smith"]})
print(df.columns.tolist())2Practical Example
Here is a real-world application of df.columns showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"a": [1], "b": [2]})
df.columns = ["first", "second"]
print(df.columns.tolist())3Best Practices
Follow these guidelines when working with df.columns:
1. Use df.rename(columns={...}) for renaming just a few specific columns by their existing name, rather than reassigning the entire columns list
2. Reassign df.columns directly only when replacing every column name at once with a full, matching-length list
3. Convert df.columns to a plain list with .tolist() when you need to pass it somewhere that expects an ordinary Python list rather than an Index
Tip: Assigning a whole new list to df.columns replaces every column name at once and requires the new list to have exactly the same length as the current number of columns — use df.rename(columns={...}) instead when you only want to rename a few specific columns by name.
import pandas as pd
df = pd.DataFrame({"first_name": ["Alice"], "last_name": ["Smith"]})
print(df.columns.tolist())