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)
相关推荐
jyOverQ16 小时前
LangGraph:子图动态路由与 Agent 工作流设计
python·langchain
晴天1616 小时前
Gradio 与 Streamlit:Python 快速构建 Web 应用的双子星-Day28
开发语言·前端·python
苏灿烤鱼16 小时前
把求职写成工作流,公开 fork 为什么会把简历写进仓库?
python·typescript·claude
whcyhhh16 小时前
头歌实践教学平台:数据科学与大数据技术导论(十)
大数据·开发语言·python
华研前沿标杆游学20 小时前
华为松山湖高阶研学|企业数字化转型游学攻略
python
Data_Journal1 天前
用于网页抓取的 Node-unblocker
大数据·开发语言·数据库·python·scrapy
豆角焖肉1 天前
SSM 聚合项目搭建:多 Maven 模块项目
开发语言·python·pycharm
青 春 记 忆1 天前
零基础入门python20:Flask配置、扩展和蓝图拆分
python·flask·后端开发
m0_380743871 天前
为 OpenAI 兼容接口配置教程
开发语言·python·node.js
leisoo80971 天前
ig50数据落盘ClickHousevsTimescaleDBvsDuckDB实测对比 IG50免费开源股票数据API接口
开发语言·jvm·数据库·python·json