columns(): List of Column names of DataFrame

Pandas

We can create one DataFrame by using dictionary or by reading any Excel or reading CSV file. We will display the column names of this dataframe here.

By using columns

import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,60,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data.columns) # Object
Output
Index(['NAME', 'ID', 'MATH', 'ENGLISH'], dtype='object')
Getting any perticular column
print(my_data.columns[2]) # Math 
By using keys()
print(my_data.keys())
Output
Index(['NAME', 'ID', 'MATH', 'ENGLISH'], dtype='object')
By using for loop
for cols in my_data.columns:
  print(cols) 
Output
NAME
ID
MATH
ENGLISH
To get data from each column of DataFrame
for cols in my_data.columns:
  print(my_data[cols])
Calling a function by passing each column as parameter and display data.
def my_fun(cols): # function to receive column name as parameter 
    print(my_data[cols])
for cols in my_data.columns:
    my_fun(cols)
By using list
print(list(my_data.columns))
Output
['NAME', 'ID', 'MATH', 'ENGLISH']
By using tolist()
print(my_data.columns.values.tolist())
Output
['NAME', 'ID', 'MATH', 'ENGLISH']
Pandas DataFrame describe() head() rename()

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer