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)
相关推荐
liliangcsdn13 分钟前
基于信号强度的稀疏选股算法的探索分析
开发语言·python
小白快快跑哦27 分钟前
python-字符串全解(四):字符串方法
开发语言·python·字符串
用户2986985301434 分钟前
Python 实现文本文件与 Word 文档互转的两种方法
后端·python·api
Zane199438 分钟前
明明在赋值前读取,为什么还会报 UnboundLocalError?global 与 nonlocal 深挖
后端·python
小毛驴85042 分钟前
Jupyter Notebook 安装步骤
ide·python·jupyter
BUG研究员_1 小时前
LangGraph节点缓存-CachePolicy
python·缓存·langraph
2601_953988071 小时前
Ricon组态实时监控 - 毫秒级数据可视化
前端·物联网·数学建模·信息可视化·架构·前端框架
quantdash_cc1 小时前
如何设计高效率的批量股票数据获取程序?QuantDash Python SDK 全市场行情拉取实战指南
开发语言·python·数据分析·量化交易·股票数据·quantdash
砚底藏山河1 小时前
拉数管道设计:从一次性脚本到可重启的数据流水线(魔码量化实战 #01)
java·数据库·python·金融·maven