How to get the list of column headers from a Pandas DataFrame?

PandasServer Side ProgrammingProgramming

To get a list of Pandas DataFrame column headers, we can use df.columns.values.

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

  • Print the input DataFrame.

  • Print the list of df.columns.values output.

Example

 Live Demo

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 1, 9],
      "y": [4, 1, 5, 10],
      "z": [4, 1, 5, 0]
   }
)

print "Input DataFrame is:\n", df
print "List of headers are: ", list(df.columns.values)

Output

Input DataFrame is:
   x  y  z
0  5  4  4
1  2  1  1
2  1  5  5
3  9 10  0
List of headers are: ['x', 'y', 'z']
raja
Published on 30-Aug-2021 09:23:42
Advertisements