Table of Contents
When importing an Excel file into Pandas, it is possible to set column names in a few simple steps. First, use the “read_excel” function to read the Excel file into a Pandas DataFrame. Then, access the “columns” attribute of the DataFrame and assign it a list of desired column names. Finally, use the “rename” function to rename the columns in the DataFrame. By following these steps, one can easily set column names when importing an Excel file into Pandas.
Pandas: Set Column Names when Importing Excel File
You can use the following basic syntax to set the column names of a DataFrame when importing an Excel file into pandas:
colnames = ['col1', 'col2', 'col3'] df = pd.read_excel('my_data.xlsx', names=colnames)
The names argument takes a list of names that you’d like to use for the columns in the DataFrame.
By using this argument, you also tell pandas to use the first row in the Excel file as the first row in the DataFrame instead of using it as the header row.
The following example shows how to use this syntax in practice.
Example: Set Column Names when Importing Excel File into Pandas
Suppose we have the following Excel file called players_data.xlsx:

From the file we can see that the first row does not contain any column names.
If we import the Excel file using the read_excel() function, pandas will attempt to use the values in the first row as the column names for the DataFrame:
import pandas as pd #import Excel file df = pd.read_excel('players_data.xlsx') #view resulting DataFrame print(df) A 22 10 0 B 14 9 1 C 29 6 2 D 30 2 3 E 22 9 4 F 31 10
However, we can use the names argument to specify our own column names when importing the Excel file:
import pandas as pd #specify column names colnames = ['team', 'points', 'rebounds'] #import Excel file and use specified column names df = pd.read_excel('players_data.xlsx', names=colnames) #view resulting DataFrame print(df) team points rebounds 0 B 14 9 1 C 29 6 2 D 30 2 3 E 22 9 4 F 31 10
Notice that the first row in the Excel file is no longer used as the header row.
Instead, the column names that we specified using the names argument are now used as the column names.
Note: You can find the complete documentation for the pandas read_excel() function .
Cite this article
stats writer (2024). How can I set column names when importing an Excel file into Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-set-column-names-when-importing-an-excel-file-into-pandas/
stats writer. "How can I set column names when importing an Excel file into Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-set-column-names-when-importing-an-excel-file-into-pandas/.
stats writer. "How can I set column names when importing an Excel file into Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-set-column-names-when-importing-an-excel-file-into-pandas/.
stats writer (2024) 'How can I set column names when importing an Excel file into Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-set-column-names-when-importing-an-excel-file-into-pandas/.
[1] stats writer, "How can I set column names when importing an Excel file into Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I set column names when importing an Excel file into Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
