Read and print column names of Pandas dataframe

In your development and dealing with huge datasets you may need to read column names of data.

Python has very powerful library called as pandas for data processing, pandas have very popular data structure named as Dataframe.

There are many ways to get column names of dataframe in pandas library we below:


import pandas as pd

df = pd.DataFrame({'column1': [2, 4, 8, 0],
                   'column2': [6, 0, 0, 0],
                   'column3': [10, 2, 1, 8],
                   'column4': [14, 5, 32, 68]})

# print Dataframe
print(df)

# Column names as list
print(list(df.columns))

# Column names
print(df.columns.values)

# iterating the columns 
for column in df.columns: 
    print(column, end=' ') # using end argument to print all column values in single line.

print()
# Using toList() function
print(df.columns.values.tolist())

Happy coding.

Comments

Popular posts from this blog

Pinescript Code for Hilega Milega

Sending message to Telegram Group, User or Bot using Python

Pine script for Intraday backtesting Previous Day high and Previous Day Low Breakout strategy