Wednesday, April 24, 2024
HomejQueryGet Column Names by Pandas Information Body Columns Property

Get Column Names by Pandas Information Body Columns Property


The best way to get column labels in a Information Body

Pandas Information Body has the columns property that you could be use to get the column labels in an information body.

Syntax:

DataFrame.columns

Within the instance beneath, we’ll present you getting column labels of information frames which are created based mostly on CSV and Excel recordsdata.

An instance to get column labels of a CSV file

Within the instance beneath, we’ll load a CSV file through the use of the read_csv technique of Pandas.

Our pattern CSV file is proven beneath:

That is used to create an information body.

That is adopted through the use of the columns property of the DF to show the column labels within the CSV file:

Code:



Output:

Index([‘Product ID’, ‘Product Name’, ‘Price’, ‘Status’], dtype=’object’)

The instance of utilizing an excel file and displaying its columns

This time, an information body is created through the use of the read_excel technique of Padnas.

Pattern Excel sheet:

pandas-df-excel-cols

Then columns property of the Information Body is used to show the labels within the knowledge body.

Python program:



Consequence:

Index([‘Product ID’, ‘Product Name’, ‘Price’, ‘Status’], dtype=’object’)

The instance of making an information body and utilizing columns property

Within the Python program beneath, we’ve created a Pandas Information body with three columns and likewise created three rows of information.

That is adopted by displaying the info body.

Lastly, we used the columns property of the DF to show column labels of the info body.

Code:



Output:

Index([‘Year’, ‘Inflation’], dtype=’object’)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments