Get Column Names as List in Pandas DataFrame
Python Pandas, the short form from Panel Data (3D container of dataset), is a python library which contains in-built methods to manipulate, transform, visualize and analyze data. The data in the Pandas columns can contain alpha-numerical characters or logical data and can be of the similar type. The following programs illustrate to get DataFrame column headers using various methods. The fastest and simplest way to get column header name is:
DataFrame.columns.values.tolist()
examples:
Create a Pandas DataFrame with data
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben']
df['TotalMarks'] = [82, 38, 63,22,55,40]
df['Grade'] = ['A', 'E', 'B','E','C','D']
df['Promoted'] = [True, False,True,False,True,True]
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
3 Jim 22 E False
4 Harry 55 C True
5 Ben 40 D True
Get list of column headers from a Pandas DataFrame
df.columns.values.tolist()
['Students_name', 'TotalMarks', 'Grade', 'IsPromoted']
Or
list(df.columns)
['Students_name', 'TotalMarks', 'Grade', 'IsPromoted']
If you want column header names in Python Tuple :
*df,
('Students_name', 'TotalMarks', 'Grade', 'IsPromoted')
When you use *df, please note the trailing comma.
If you want column header names in Python Set :
{*df}
{'Grade', 'IsPromoted', 'Students_name', 'TotalMarks'}
If you want column header names in Python List :
[*df]
['Students_name', 'TotalMarks', 'Grade', 'IsPromoted']
Pandas DataFrame follows the dict-like convention of iterating over the "keys" of the objects. So you can get column header names as:
df.keys()
Index(['Students_name', 'TotalMarks', 'Grade', 'IsPromoted'], dtype='object')
If you want sorted column header names :
sorted(df)
['Grade', 'IsPromoted', 'Students_name', 'TotalMarks']
It is important to note that sorted(df) does not preserve the original order of the dataframe column header names. For that, you should use list(df) instead.
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame