Get List of Column Names Grouped by Data Type in Python (Example)

 

This page illustrates how to return a list of column names grouped by the type of the column in Python.

The content of the page is structured as follows:

It’s time to dive into the example…

 

Example Data & Add-On Libraries

First, we need to load the pandas library:

import pandas as pd                                    # Import pandas library in Python

We also need to create some data that we can use in the example syntax below:

data = pd.DataFrame({'x1':['a', 'b', 'c', 'd', 'e'],  # Create example DataFrame
                     'x2':range(6, 1, - 1),
                     'x3':[2, 5, 5, 8, 3],
                     'x4':['x', 'x', 'x', 'x', 'x'],
                     'x5':range(1, 6)})
print(data)                                           # Print example DataFrame

 

table 1 DataFrame get list column names grouped data type python

 

Have a look at the previous table. It shows that our example data consists of five rows and the five columns “x1”, “x2”, “x3”, “x4”, and “x5”.

 

Example: Return Column Names Grouped by Data Types Using to_series() & groupby() Functions

This example shows how to create a list containing the variable names of a pandas DataFrame sorted by data type group.

For this, we can apply the to_series function in combination with the groupby() function.

Have a look at the following Python syntax and its output:

print(data.columns.to_series().groupby(data.dtypes).groups)
# {int64: ['x2', 'x3', 'x5'], object: ['x1', 'x4']}

As you can see, we have created a Python list containing the column names of our pandas DataFrame ordered by data type

 

Video, Further Resources & Summary

Do you need more explanations and details on the content of this post? Then you should have a look at the video tutorial of the Statistics Globe YouTube channel. In the video, I explain the Python programming code of this article:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Still not enough? Then you might want to have a look at the following video on the YouTube channel of Erik Marsja. He demonstrates in six different examples how to get the column names of a pandas DataFrame using the Python programming language:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Besides the video, you may want to read some of the other tutorials on this homepage. You can find some articles below.

 

Summary: On this page, I have explained how to return a list of column names grouped by the data type in the Python programming language. If you have any further questions and/or comments, tell me about it in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top