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)
相关推荐
IT=>小脑虎10 分钟前
Python爬虫零基础学习知识点详解【基础版】
爬虫·python·学习
做萤石二次开发的哈哈1 小时前
萤石开放平台 萤石可编程设备 | 设备 Python SDK 使用说明
开发语言·网络·python·php·萤石云·萤石
知乎的哥廷根数学学派1 小时前
基于多物理约束融合与故障特征频率建模的滚动轴承智能退化趋势分析(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习
HarmonLTS2 小时前
Python Socket网络通信详解
服务器·python·网络安全
郝学胜-神的一滴2 小时前
Python数据封装与私有属性:保护你的数据安全
linux·服务器·开发语言·python·程序人生
专注数据的痴汉2 小时前
「数据获取」内蒙古地理基础数据(道路、水系、四级行政边界、地级城市、DEM等)
大数据·人工智能·信息可视化
智航GIS2 小时前
11.7 使用Pandas 模块中describe()、groupby()进行简单分析
python·pandas
Pyeako2 小时前
机器学习--矿物数据清洗(六种填充方法)
人工智能·python·随机森林·机器学习·pycharm·线性回归·数据清洗
ScilogyHunter3 小时前
SCons:Python驱动的智能构建系统
python·构建系统·scons