While analyzing the real datasets which are often very huge in size, we might need to get the column names in order to perform some certain operations.
Let’s discuss how to get column names in Pandas dataframe.
First, let’s create a simple dataframe with nba.csv file.
# Import pandas package import pandas as pd # making data frame # calling head() method # storing in new variable data_top = data.head() # display data_top |

Now let’s try to get the columns name from above dataset.
Method #1: Simply iterating over columns
# Import pandas package import pandas as pd # making data frame data = pd.read_csv("nba.csv") # iterating the columns for col in data.columns: print(col) |
Output:

Method #2: Using columns with dataframe object
# Import pandas package import pandas as pd # making data frame data = pd.read_csv("nba.csv") # list(data) or list(data.columns) |
Output:

Method #3: column.values method returs an array of index.
# Import pandas package import pandas as pd # making data frame data = pd.read_csv("nba.csv") list(data.columns.values) |
Output:

Method #4: Using tolist() method with values with given the list of columns.
# Import pandas package import pandas as pd # making data frame data = pd.read_csv("nba.csv") list(data.columns.values.tolist()) |
Output:

Method #5: Using sorted() method
Sorted() method will return the list of columns sorted in alphabetical order.
# Import pandas package import pandas as pd # making data frame data = pd.read_csv("nba.csv") # using sorted() method sorted(data) |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- How to get column and row names in DataFrame?
- Python | Change column names and row indexes in Pandas DataFrame
- How to lowercase column names in Pandas dataframe
- How to Sort a Pandas DataFrame based on column names or row index?
- Add column names to dataframe in Pandas
- Get column index from column name of a given Pandas DataFrame
- How to get rows/index names in Pandas dataframe
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- Remove spaces from column names in Pandas
- Pandas - Remove special characters from column names
- Get unique values from a column in Pandas DataFrame
- Get n-smallest values from a particular column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
- Split a column in Pandas dataframe and get part of it
- Get a list of a particular column values of a Pandas DataFrame
- Get a list of a specified column of a Pandas DataFrame
- Get list of column headers from a Pandas DataFrame
- Create a DataFrame from a Numpy array and specify the index column and column headers
- Drop columns in DataFrame by label Names or by Index Positions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

