pandas操作excel操作-02-数据读取

我的未来我决定 提交于 2020-02-29 12:31:14
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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!