python_数据可视化_pandas_导入excel数据

目录

1.1导入库

1.2读取excel文件

1.3读取excel,指定sheet2工作表

1.4指定行索引

1.5指定列索引

1.6指定导入列

案例速览:

1.1导入库

python 复制代码
import pandas as pd

1.2读取excel文件

pd.read_excel('文件路径')

python 复制代码
data = pd.read_excel('D:/desktop/TestExcel.xlsx')

print(data)

1.3读取excel,指定sheet2工作表

pd.read_excel('文件路径',sheet_name='工作表名称')

注意:sheet_name也可以按照索引去访问工作表,比如等于0的时候表示第一个工作表

python 复制代码
data = pd.read_excel('D:/desktop/TestExcel.xlsx',sheet_name='Sheet2')

print(data)

1.4指定行索引

pd.read_excel('文件路径',index_col=0)

注意:index_col可以按照索引去访问列,比如等于0的时候表示第一列

python 复制代码
data = pd.read_excel('D:/desktop/TestExcel.xlsx',index_col=0)

print(data)

1.5指定列索引

pd.read_excel('文件路径',header=0)

注意:header可以按照索引去访问行,比如等于0的时候表示第一行

使用默认的列索引,可以直接将header=None

python 复制代码
data = pd.read_excel('D:/desktop/TestExcel.xlsx',header=1)

print(data)

1.6指定导入列

pd.read_excel('文件路径',usecols=2)

注意:usecols可以按照索引去访问列,比如等于2的时候表示访问前三列

按理来说是这么一回事,但是会报错

python 复制代码
data = pd.read_excel('D:/desktop/TestExcel.xlsx',sheet_name='Sheet1',usecols=2)

print(data)

ValueError: Passing an integer for `usecols` is no longer supported. Please pass in a list of int from 0 to `usecols` inclusive instead.

大概意思是要传入一个整数列表,所以如果想要取出前三列,就要这么写

python 复制代码
data = pd.read_excel('D:/desktop/TestExcel.xlsx',sheet_name='Sheet1',usecols=[0,1,2])

print(data)
相关推荐
2601_9628857230 分钟前
如何用 Python 计算 VaR 和 CVaR(在险价值)?
开发语言·python
李航198338 分钟前
DeepSeekAI建模三 - 实战建模一组柜子
人工智能·python·计算机视觉·ai·ai编程
严谨的麻辣烫2 小时前
静态代理 IP 的出口一致性如何验证?Python 请求检测与异常排查
开发语言·python·http·网络安全
我命由我123452 小时前
5-1.Python 编码进阶 - 函数进阶(参数、返回值、递归、函数作为参数传递、lambda 匿名函数)
java·服务器·数据库·后端·python·java-ee·python3.11
FITA阿泽要努力2 小时前
Fork Agent:共享上下文的艺术
开发语言·python·agent
DeeGLMath3 小时前
optimtool最新版研究梯度优化
python·optimtool·梯度优化
坐吃山猪3 小时前
Python-Hermite基函数
开发语言·python
空~自由3 小时前
Python 正则 . * ? .* .*? (.*?) 通俗讲解
开发语言·python
Web3&Basketball3 小时前
DeepSeek V4.1 Flash 实测:Token 砍半、Search API 跑通
数据库·人工智能·python·大模型·api调用·deepseek·推理优化
墨子夕3 小时前
设计模式——Java × Python 双语言精讲 · 23 个经典模式 + 现代演进(第一篇)
java·python·设计模式