- Pandas Get Column Names from DataFrame
- 1. Quick Examples of Get Column Names
- 2. pandas Get Column Names
- 3. Use list(df) to Get Column Names from DataFrame
- 4. Get Column Names in Sorting order
- 5. Access All Column Names by Iterating
- 6. Get Column Headers Using the keys() Method
- 7. Get All Numeric Column Names
- 9. Complete Example of pandas Get Columns Names
- Conclusion
- Related Articles
- References
- You may also like reading:
- How to Get Column Names of Pandas DataFrame?
- Examples
- 1. Print DataFrame column names
- 2. Access individual column names using index
- 3. Print column names using For loop
- Summary
- How to Get Column Names in Pandas?
- pandas Get Column Names
- Get Column Names in Sorting order
- Example of pandas Get Columns Names
- Conclusion
Pandas Get Column Names from DataFrame
How to get or print Pandas DataFrame Column Names? You can get the Pandas DataFrame Column Names by using DataFrame.columns.values method and to get it as a list use tolist(). Each column in a Pandas DataFrame has a label/name that specifies what type of value it holds/represents. Getting a column names is useful when you wanted to access all columns by name programmatically or manipulate the values of all columns. In this article, I will explain different ways to get column names from pandas DataFrame headers with examples.
To get a list of columns from the DataFrame header use DataFrame.columns.values.tolist() method. Below is an explanation of each section of the statement.
- .columns returns an Index object with column names. This preserves the order of column names.
- .columns.values returns an array and this has a helper function .tolist() that returns a list of column names.
1. Quick Examples of Get Column Names
Following are some quick examples of how to get column names from pandas DataFrame, If you wanted to print it to console just use the print() statment.
Create a Pandas DataFrame from Dict with a few rows and with columns names Courses , Fee , Duration and Discount .
df = pd.DataFrame(technologies) print(df)
2. pandas Get Column Names
You can get the column names from pandas DataFrame using df.columns.values , and pass this to python list() function to get it as list, once you have the data you can print it using print() statement. I will take a moment to explain what is happening on this statement, df.columns attribute returns an Index object which is a basic object that stores axis labels. Index object provides a property Index.values that returns data in an array, in our case it returns column names in an array.
Note that df.columns preserve the order of the columns as-is.
To convert an array of column names into a list, we can use either .toList() on array object or use list(array object) .
You can also use df.columns.values.tolist() to get the DataFrame column names.
3. Use list(df) to Get Column Names from DataFrame
Use list(df) to get the column header from pandas DataFrame. You can also use list(df.columns) to get column names.
4. Get Column Names in Sorting order
In order to get a list of column names in a sorted order use sorted(df) function. this function returns column names in alphabetical order.
Yields below output. Notice the difference of output from above.
5. Access All Column Names by Iterating
Sometimes you may need to iterate over all columns and apply some function, you can do this as below.
6. Get Column Headers Using the keys() Method
df.keys() is another approach to get all column names as a list from pandas DataFrame.
7. Get All Numeric Column Names
Sometimes while working on the analytics, you may need to work only on numeric columns, hence you would be required to get all columns of a specific data type. For example, getting all columns of numeric data type can get using undocumented function df._get_numeric_data() .
Use for df.dtypes[df.dtypes!=»Courses»].index : This is another simple code for finding numeric columns in a pandas DataFrame.
Yields same output as above.
9. Complete Example of pandas Get Columns Names
df = pd.DataFrame(technologies) print(df) # Get the list of all column names from headers column_headers = list(df.columns.values) print("The Column Header :", column_headers) # Get the list of all column names from headers column_headers = df.columns.values.tolist() print("The Column Header :", column_headers) # Using list(df) to get the column headers as a list column_headers = list(df.columns) # Using list(df) to get the list of all Column Names column_headers = list(df) # Dataframe show all columns sorted list col_headers=sorted(df) print(col_headers) # Get all Column Header Labels as List for column_headers in df.columns: print(column_headers) column_headers = df.keys().values.tolist() print("The Column Header :", column_headers) # Get all numeric columns numeric_columns = df._get_numeric_data().columns.values.tolist() print(numeric_columns) # Simple Pandas Numeric Columns Code numeric_columns=df.dtypes[df.dtypes == "int64"].index.values.tolist() print(numeric_columns)
Conclusion
In this article, you have learned how to get or print the column names using df.columns , list(df) , df.keys , and also learned how to get all column names of type integer, finally getting column names in a sorted order e.t.c
Related Articles
References
You may also like reading:
How to Get Column Names of Pandas DataFrame?
To get the column names of DataFrame, use DataFrame.columns property.
The syntax to use columns property of a DataFrame is
The columns property returns an object of type Index. We could access individual names using any looping technique in Python.
Examples
1. Print DataFrame column names
In this example, we get the dataframe column names and print them.
Python Program
import pandas as pd # Initialize a DataFrame df = pd.DataFrame( [['Amol', 72, 67, 91], ['Lini', 78, 69, 87], ['Kiku', 74, 56, 88], ['Ajit', 54, 76, 78]], columns=['name', 'physics', 'chemistry', 'algebra']) # Get the DataFrame column names cols = df.columns # Print the column names print(cols)
Index(['name', 'physics', 'chemistry', 'algebra'], dtype='object')
2. Access individual column names using index
You can access individual column names using the index.
Python Program
import pandas as pd # Initialize a DataFrame df = pd.DataFrame( [['Amol', 72, 67, 91], ['Lini', 78, 69, 87], ['Kiku', 74, 56, 88], ['Ajit', 54, 76, 78]], columns=['name', 'physics', 'chemistry', 'algebra']) # Get the DataFrame column names cols = df.columns # Print the column names for i in range(len(cols)): print(cols[i])
name physics chemistry algebra
3. Print column names using For loop
You can use a For loop to iterate over the column names of DataFrame.
Python Program
import pandas as pd # Initialize a dataframe df = pd.DataFrame( [['Amol', 72, 67, 91], ['Lini', 78, 69, 87], ['Kiku', 74, 56, 88], ['Ajit', 54, 76, 78]], columns=['name', 'physics', 'chemistry', 'algebra']) # Get the DataFrame column names cols = df.columns # Print the column names using For loop for column in cols: print(column)
name physics chemistry algebra
Summary
In this Pandas Tutorial, we extracted the column names from DataFrame using DataFrame.column property.
How to Get Column Names in Pandas?
In this article, we will learn how to get the column name from a given dataset in pandas. We have previously learned that Pandas is a Python library used for working with data sets. It offers tools for data exploration, cleaning, analysis, and manipulation.
How to get or print Pandas DataFrame Column Names?
DataFrame.columns.values method can be used to obtain the Pandas DataFrame Column Names, and to-list can be used to obtain the names as a list (). Each column in a Pandas DataFrame has a label or name that indicates the kind of value the column contains or represents. Obtaining the column names is helpful if you wish to change the values of all columns or have programmatic access to all columns by name. I’ll describe various approaches to extracting column names from Pandas DataFrame headers in this article along with examples.
Use DataFrame.columns.values.tolist() to retrieve a list of columns from the DataFrame header. The statement’s many parts are individually explained below.
- .columns returns an Index object with column names. This preserves the order of column names.
- .columns.values returns an array and this has a helper function .tolist() that returns a list of column names.
The examples that follow show you how to quickly retrieve column names from a pandas DataFrame. To output it to the terminal, simply use the print() statement.
# Below are some quick examples
# Get the list of all column names from headers
column_names = list (df.columns.values)
# Get the list of all column names from headers
column_names = df.columns.values.tolist()
#Using list (df) to get the column headers as a list
column_names = list (df.columns)
#Using list (df) to get the list of all Column Names
column_names = list (df)
# Dataframe show all columns sorted list
column_names=sorted(df)
# Get all Column Header Labels as List
for column_headers in df.columns:
print(column_headers)
column_names = df.keys().values.tolist()
# Get all numeric columns
numeric_columns = df._get_numeric_data().columns.values.tolist()
# Simple Pandas Numeric Columns Code
numeric_columns=df.dtypes[df.dtypes == «int64» ].index.values.tolist()
Create a Pandas DataFrame from Dict with a few rows and with columns names Courses, Fee, Duration, and Discount.
import pandas as pd
import numpy as np
technologies= <
‘Courses’ :[ «Spark» , «PySpark» , «Hadoop» , «Python» , «Pandas» ],
‘Fee’ :[ 22000 , 25000 , 23000 , 24000 , 26000 ],
‘Duration’ :[ ’30days’ , ’50days’ , ’30days’ , None,np.nan],
‘Discount’ :[ 1000 , 2300 , 1000 , 1200 , 2500 ]
>
df = pd.DataFrame(technologies)
print(df)
pandas Get Column Names
With the help of the Python list() function, you can obtain the column names from a Pandas DataFrame. Once you have the data, you can print it using the print() statement. This statement’s df.columns attribute produces an Index object, a fundamental object that contains axis labels, so let me briefly explain what is happening. In our example, the property Index.values of the Index object gives column names in an array of data.
Keep in mind that df.columns maintains the columns’ original arrangement. Either one can be used to turn an array of column names into a list.
Use list or toList() on an array object (array object).
# Get the list of all column names from headers
column_headers = list (df.columns.values)
print( «The Column Header :» , column_headers)
The Column Header : [ ‘Courses’ , ‘Fee’ , ‘Duration’ , ‘Discount’ ]
You can also use df.columns.values.tolist() to get the DataFrame column names.
# Get the list of all column names from headers
column_headers = df.columns.values.tolist()
print( «The Column Header :» , column_headers)
Get Column Names in Sorting order
Use the sorted(df) function to obtain a list of column names in sorted order. column names are returned by this function in alphabetical order.
# Dataframe show all columns sorted list
col_headers=sorted(df)
print(col_headers)
Yields below output. Notice the difference of output from above.
[ ‘Courses’ , ‘Discount’ , ‘Duration’ , ‘Fee’ ]Access All Column Names by Iterating
You can perform the iteration over all columns and application of a function as shown below.
# Get all Column Header Labels as List
for column_headers in df.columns:
print(column_headers)
Courses
Fee
Duration
Discount
Example of pandas Get Columns Names
import pandas as pd
import numpy as np
technologies= <
‘Courses’ :[ «Spark» , «PySpark» , «Hadoop» , «Python» , «Pandas» ],
‘Fee’ :[ 22000 , 25000 , 23000 , 24000 , 26000 ],
‘Duration’ :[ ’30days’ , ’50days’ , ’30days’ , None,np.nan],
‘Discount’ :[ 1000 , 2300 , 1000 , 1200 , 2500 ]
>
df = pd.DataFrame(technologies)
print(df)
# Get the list of all column names from headers
column_headers = list (df.columns.values)
print( «The Column Header :» , column_headers)
# Get the list of all column names from headers
column_headers = df.columns.values.tolist()
print( «The Column Header :» , column_headers)
#Using list (df) to get the column headers as a list
column_headers = list (df.columns)
#Using list (df) to get the list of all Column Names
column_headers = list (df)
# Dataframe show all columns sorted list
col_headers=sorted(df)
print(col_headers)
# Get all Column Header Labels as List
for column_headers in df.columns:
print(column_headers)
column_headers = df.keys().values.tolist()
print( «The Column Header :» , column_headers)
# Get all numeric columns
numeric_columns = df._get_numeric_data().columns.values.tolist()
print(numeric_columns)
# Simple Pandas Numeric Columns Code
numeric_columns=df.dtypes[df.dtypes == «int64» ].index.values.tolist()
print(numeric_columns)
Conclusion
In this article, you learned how to get or print the column names using the functions df.columns, list(df), and df.keys. You also learned how to get the names of all the integer columns and how to get the column names sorted, among other things.