Get list of column headers from a Pandas DataFrame

Get list of column headers from a Pandas DataFrame

To get a list of column headers from a Pandas DataFrame, you can simply use the columns attribute of the DataFrame and then convert it to a list if needed.

Here's how to do it:

import pandas as pd # Sample DataFrame data = { 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] } df = pd.DataFrame(data) # Get column headers column_headers = df.columns # Convert to a list (if needed) column_list = list(column_headers) print(column_list) # Outputs: ['A', 'B', 'C'] 

The columns attribute returns an Index object, which can be easily converted to a list using the list() function.


More Tags

backbone.js typeface scrollview magrittr curve-fitting dual-sim macos-high-sierra super count ssis-2012

More Programming Guides

Other Guides

More Programming Examples