This is how to print column names from your dataframe in Pandas.
Listing the column names is one of the first things you do when you look at your data. It’s natural that you want to know the data structure, data size, data types in the beginning.
In case the table contains many columns, you are not able to print them, so you want to at least print their names.
You may think that printing column names is trivial. However, as you’ll see, printing column names isn’t easy at all, as you may run into a few problems that I’ll help you solve.
How to print column names
To list the column names, simply use a combination of the existing Pandas functions columns, values, and to_list.
This is my sample dataframe whose columns I would like to print out.
import pandas as pd my_df = pd.DataFrame({'id':['id1','id2','id3'], 'Column1': ['2', '7', '6'], 'Column2': ['4', '5', '8'], 'Column3': ['4', '1', '9'], 'Column4': ['3', '3', '8'], 'Column5': ['2', '5', '4'], 'Column6': ['2', '7', '3']})
Use combinations of the column names listed above to get the column names.
import pandas as pd my_df = pd.DataFrame({'id':['id1','id2','id3'], 'Column1': ['2', '7', '6'], 'Column2': ['4', '5', '8'], 'Column3': ['4', '1', '9'], 'Column4': ['3', '3', '8'], 'Column5': ['2', '5', '4'], 'Column6': ['2', '7', '3']}) columns = my_df.columns.values.tolist() print(f"List of the columns: \n{columns}")
Pandas printed out the list of columns as I needed it.
How to solve column name printing problems
As I mentioned, the task is not easy, because it is easy to come across problems, i.e. AttributeError, which I pasted below.
Traceback (most recent call last): File "C:\Users\panda\PycharmProjects\venv\column_names.py", line 11, in columns = pd.DataFrame.columns.values.tolist() AttributeError: 'pandas._libs.properties.AxisProperty' object has no attribute 'values' Process finished with exit code 1
The error occurred because I incorrectly called a combination of functions in Pandas.
columns = pd.DataFrame.columns.values.tolist()
As you can see, instead of referring to the dataframe, I called the columns function on the DataFrame function, which is incorrect.
The proper form is to call my_df.columns, and that’s the only way you should use it to avoid getting an AttributeError.
Handling Common Errors
Here’s how to properly access column names to avoid this error:
Incorrect usage:
columns = pd.DataFrame.columns.values.tolist()
Correct usage:
columns = my_df.columns.values.tolist()
The AttributeError occurs because you inadvertently called the columns function on the DataFrame class itself, rather than on an instance of a DataFrame, such as my_df. The correct approach is to access the columns attribute on your DataFrame instance (my_df in this case) to retrieve the column names.
By following the correct method, you can avoid this common error and successfully print the column names of your DataFrame.
Pingback: How To Print A Row • Pandas How To