import pandas as pd
# 读取excel文件
# header 默认是0,表示第一行数据作为columns,如果前面有其他的内容,可以通过设置header的行数解决
# 如果excel前面都是空行,都可以不用设置header的值
# 读取excel文件的时候,指定使用某列作为索引列,如果不指定的话,会生成新的索引列
people_df = pd.read_excel('D:/output.xlsx', header=0, index_col="ID")
# 查看数据有多少行,多少列
print(people_df.shape)
# 查看所有的列名
print(people_df.columns)
# 查看前10行数据
print(people_df.head(10))
# 查看最后3行数据
print(people_df.tail(3))
读取 excel中的数据,处理没有列名的数据,使用某列作为index
# 处理没有列名的数据
people_df = pd.read_excel('D:/output.xlsx', header=None)
# 设置列名
people_df.columns = ['idx','ID2', 'Name2']
# 设置使用某列作为索引列, inplace=True 表示直接在现在的DataFrame修改,不生成新的DataFrame
people_df.set_index('idx', inplace=True)
people_df.to_excel('D:/output.xlsx')
print('Done!')
视频观看链接:https://www.bilibili.com/video/av88814463?p=2
来源:oschina
链接:https://my.oschina.net/ski/blog/3179374