pandas读取excel文件

本文介绍了如何利用Python的pandas库读取和处理Excel文件,包括加载people.xlsx数据,进行数据操作,以及将结果保存到output.xlsx和output2.xlsx中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import pandas as pd


# 先将文件读到内存中形成一个datefream 
people = pd.read_excel(r'People.xlsx')
# 查看行数和列数
print(people.shape)
# 查看列名
# 当直接打印datefream时 ID不会显示(datefream对待index和columns是分开对待的)
print(people.columns)
# 打印表中的前三行 默认为五行
print(people.head(3))
print('=========================')
# 查看末尾三行 默认为五行
print(people.tail(3))

# # pandas读取excel默认从第0行开始读取数据 设置head=1 默认为0
# # 但表格文件第一行为空时 会自动跳过(header不脏的时候)
# people = pd.read_excel(r'People.xlsx', header=1)

# 当不设置header时 会自动生成int类型的header
# 设置herder
people.columns = ('ID', 'Type', 'Title', 'FirstName', 'MiddleName', 'LastName')
# 不使用自动添加的索引列 使用ID列作为索引列
people.set_index('ID',inplace=True)
# 将设置好的数据存放到下面表格(output.xlsx)中
people.to_excel(r'output.xlsx')
print("It's OK ")

# 将自动打印的index去掉
# yi
people.set_index('ID')
# er
# 当 inplace=True 时 直接修改当前的datefream 不会再新添加index
people.set_index('ID',inplace=True)

# df = pd.read_excel(r'output.xlsx')
# # 当再次读取output时 发现id列又变成了普通列而不是index列 自动生成index
# print(df.head())

#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值