获取Pandas读取Excel后所有列名的几种方法

import pandas as pd

data = pd.read_excel('客户信息.xlsx')
# 获取Pandas读取Excel后所有列名的几种方法
print(list(data)) # 0. 直接使用 list 关键字,返回一个list
columns_name1 = [column for column in data] # 1.链表推倒式_获取Pandas列名的几种方法
columns_name2 = data.columns.values # 2.通过columns字段获取,返回一个numpy型的array
columns_name3 = data.columns.tolist() # 4.df.columns 返回Index,可以通过 tolist(), 或者 listarray) 转换为list

print('*' * 10)
print(columns_name1)
print('*' * 20)
print(columns_name2, type(columns_name2))
print('*' * 30)
print(columns_name3, type(columns_name2))
print('*' * 40)
print(list(columns_name3))

PyDev console: starting.
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
runfile('D:/pyexcel/temppandas.py', wdir='D:/pyexcel')
['id', 'customer_code', 'customer_name', 'delivery_address', 'purchasing_agency', 'contact_phone', 'contact_tel', 'contact_fax', 'qq', 'wechat', 'email', 'director', 'director_iphone', 'director_telephone', 'director_fax', 'registered_address', 'bank', 'account_number', 'creation_date', 'unified_social_credit_code', 'Unnamed: 20', 'office_address', 'remarks', 'creation_date.1', 'note_appended', 'client', 'credit_line', 'use_line']
**********
['id', 'customer_code', 'customer_name', 'delivery_address', 'purchasing_agency', 'contact_phone', 'contact_tel', 'contact_fax', 'qq', 'wechat', 'email', 'director', 'director_iphone', 'director_telephone', 'director_fax', 'registered_address', 'bank', 'account_number', 'creation_date', 'unified_social_credit_code', 'Unnamed: 20', 'office_address', 'remarks', 'creation_date.1', 'note_appended', 'client', 'credit_line', 'use_line']
********************
['id' 'customer_code' 'customer_name' 'delivery_address'
'purchasing_agency' 'contact_phone' 'contact_tel' 'contact_fax' 'qq'
'wechat' 'email' 'director' 'director_iphone' 'director_telephone'
'director_fax' 'registered_address' 'bank' 'account_number'
'creation_date' 'unified_social_credit_code' 'Unnamed: 20'
'office_address' 'remarks' 'creation_date.1' 'note_appended' 'client'
'credit_line' 'use_line'] <class 'numpy.ndarray'>
******************************
['id', 'customer_code', 'customer_name', 'delivery_address', 'purchasing_agency', 'contact_phone', 'contact_tel', 'contact_fax', 'qq', 'wechat', 'email', 'director', 'director_iphone', 'director_telephone', 'director_fax', 'registered_address', 'bank', 'account_number', 'creation_date', 'unified_social_credit_code', 'Unnamed: 20', 'office_address', 'remarks', 'creation_date.1', 'note_appended', 'client', 'credit_line', 'use_line'] <class 'numpy.ndarray'>
****************************************
['id', 'customer_code', 'customer_name', 'delivery_address', 'purchasing_agency', 'contact_phone', 'contact_tel', 'contact_fax', 'qq', 'wechat', 'email', 'director', 'director_iphone', 'director_telephone', 'director_fax', 'registered_address', 'bank', 'account_number', 'creation_date', 'unified_social_credit_code', 'Unnamed: 20', 'office_address', 'remarks', 'creation_date.1', 'note_appended', 'client', 'credit_line', 'use_line']
posted @ 2021-04-20 18:06  zechariah1  阅读(4015)  评论(0编辑  收藏  举报