题目4: 读取excel 文件2_1People,查看数据结构(行与列数,列名),观察数据内容(前3行与后3行)
import pandas as pd
# 题目4: 读取excel 文件2_1People,查看数据结构(行与列数,列名),观察数据内容(前3行与后3行)
people = pd.read_excel('2_1People.xlsx')
print(people.shape) # 行数和列数
print(people.columns) # 列名
print(people.head(3)) #默认显示5行,这里显示3行
print('======================')
print(people.tail(3)) #显示最后3行
题目5: 读取excel 文件2_2People,处理脏数据,指定正确的列名行
import pandas as pd
# 题目5: 读取excel 文件2_2People,处理脏数据,指定正确的列名行
people = pd.read_excel('2_2People.xlsx',header=1) # 默认第一行是列名(从0开始计数)
print(people.columns) # 列名
题目6: 读取excel 文件2_2People,处理无列名数据,自定义列名['ID','Type','Title','FirstName','MiddleName','LastName'],设置索引列ID
import pandas as pd
people = pd.read_excel('2_3People.xlsx',header=None,names=['ID','Type','Title','FirstName','MiddleName','LastName'],index_col='ID')
print(people.columns)
print(people.head())
或者:
import pandas as pd
people = pd.read_excel('2_3People.xlsx',header=None) # 读取数据后不要表头
people.columns = ['ID','Type','Title','FirstName','MiddleName','LastName'] # 自定义列名
people.set_index('ID',inplace=True) # 设置索引列(注:行索引与列索引是分开的,ID为行索引,列索引就不存在了)
print(people.head()) # 前5行