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)
相关推荐
二川bro21 小时前
Python模型优化实战:深度学习加速与压缩技巧
python
l***749421 小时前
SQL Server2022版+SSMS安装教程(保姆级)
后端·python·flask
傻啦嘿哟1 天前
Python实现PDF文档高效转换为HTML文件:从基础到进阶的完整指南
python·pdf·html
专注数据的痴汉1 天前
「数据获取」《中国商务年鉴》(2004-2024)
大数据·人工智能·信息可视化
天下无敌笨笨熊1 天前
ES作为向量库研究
大数据·python·elasticsearch
数据知道1 天前
FastAPI项目:从零到一搭建一个网站导航系统
python·mysql·fastapi·python web·python项目
程序员爱钓鱼1 天前
Python 编程实战 · 进阶与职业发展:数据分析与 AI(Pandas、NumPy、Scikit-learn)
后端·python·trae
软件开发技术深度爱好者1 天前
Python库/包/模块管理工具
开发语言·python
程序员爱钓鱼1 天前
Python 编程实战 · 进阶与职业发展:Web 全栈(Django / FastAPI)
后端·python·trae
郝学胜-神的一滴1 天前
Python中一切皆对象:深入理解Python的对象模型
开发语言·python·程序人生·个人开发