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
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()


plus2net.com



Post your comments , suggestion , error , requirements etc here




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