纳速健身

标题: Pandas读取并修改excel [打印本页]

作者: awagink    时间: 2020-10-28 23:48
标题: Pandas读取并修改excel
一、前言

最近总是和excel打交道,由于数据量较大,人工来修改某些数据可能会有点浪费时间,这时候就使用到了Python数据处理的神器—–Pandas库,话不多说,直接上Pandas。

二、安装

这次使用的python版本是python2.7,安装python可以去python的官网进行下载,这里不多说了。

安装完成后使用Python自带的包管理工具pip可以很快的安装pandas。

pip [color=rgb(0, 0, 136) !important]install pandas

如果使用的是Anaconda安装的Python,会自带pandas。

三、read_excel()介绍

首先可以先创建一个excel文件当作实验数据,名称为example.xlsx,内容如下:

[color=rgba(0, 0, 0, 0.75)]
这里是很简单的几行数据,我们来用pandas实际操作一下这个excel表。
[color=rgb(0, 153, 0) !important]# coding:utf-8import pandas as pddata = pd.read_excel('example.xlsx', sheet_name='Sheet1')[color=rgb(0, 153, 0) !important]print data

结果如下:


这里使用了read_excel()方法来读取excel,来看一个read_excel()这个方法的API,这里只截选一部分经常使用的参数:

pd.read_excel(io, sheet_name=[color=rgb(0, 102, 102) !important]0, [color=rgb(0, 0, 136) !important]header=[color=rgb(0, 102, 102) !important]0, names=[color=rgb(0, 102, 102) !important]None, index_col=[color=rgb(0, 102, 102) !important]None, usecols=[color=rgb(0, 102, 102) !important]None)

这里主要参数为io,sheet_name,header,usecols和names

io:excel文件,如果命名为中文,在python2.7中,需要使用decode()来解码成unicode字符串,例如: pd.read_excel('示例'.decode('utf-8))sheet_name:返回指定的sheet,如果将sheet_name指定为None,则返回全表,如果需要返回多个表,可以将sheet_name指定为一个列表,例如['sheet1', 'sheet2']header:指定数据表的表头,默认值为0,即将第一行作为表头。usecols:读取指定的列,例如想要读取第一列和第二列数据:    pd.read_excel("example.xlsx", sheet_name=None, usecols=[0, 1])四、使用

这里先来一个在机器学习中经常使用的:将所有gender为male的值改为0,female改为1。

[color=rgb(0, 153, 0) !important]# coding:utf-8import pandas as pdfrom pandas import DataFrame[color=rgb(0, 153, 0) !important]# 读取文件data = pd.read_excel("example.xlsx", sheet_name="Sheet1")[color=rgb(0, 153, 0) !important]# 找到gender这一列,再在这一列中进行比较data['gender'][data['gender'] == 'male'] = 0data['gender'][data['gender'] == 'female'] = 1print data

结果如下:

需要注意的是,这里的data为excel数据的一份拷贝,对data进行修改并不会直接影响到我们原来的excel,必须在修改后保存才能够修改excel。保存的代码如下:

DataFrame(data).to_excel('example.xlsx', sheet_name='Sheet1', index=False, header=True)

这时候我们再打开example.xlsx文件看看是否更改了:


如果我们想要新增加一列或者一行数据怎么办呢?这里给出参考:

新增列数据:

data['列名称'] = None

新增行数据,这里行的num为excel中自动给行加的id数值

data.loc[行的num] = [值1, 值2, ...]

以上面的数据为例:

[color=rgb(136, 0, 0) !important]# coding:utf-8[color=rgb(0, 0, 136) !important]import pandas [color=rgb(0, 0, 136) !important]as pd[color=rgb(0, 0, 136) !important]from pandas [color=rgb(0, 0, 136) !important]import DataFramedata = pd.read_excel([color=rgb(0, 153, 0) !important]"example.xlsx", sheet_name=[color=rgb(0, 153, 0) !important]'Sheet1')[color=rgb(136, 0, 0) !important]# 增加行数据,在第5行新增data.loc[[color=rgb(0, 102, 102) !important]5] = [[color=rgb(0, 153, 0) !important]'James', [color=rgb(0, 102, 102) !important]32, [color=rgb(0, 153, 0) !important]'male'][color=rgb(136, 0, 0) !important]# 增加列数据,给定默认值Nonedata[[color=rgb(0, 153, 0) !important]'profession'] = [color=rgb(0, 0, 136) !important]None[color=rgb(136, 0, 0) !important]# 保存数据DataFrame(data).to_excel([color=rgb(0, 153, 0) !important]'example.xlsx', sheet_name=[color=rgb(0, 153, 0) !important]'Sheet1', index=[color=rgb(0, 0, 136) !important]False, header=[color=rgb(0, 0, 136) !important]True)

打开excel看到的结果如下:

说完了增加一行或一列,那怎样删除一行或一列呢?

[color=rgb(0, 0, 136) !important]import pandas [color=rgb(0, 0, 136) !important]as pd[color=rgb(0, 0, 136) !important]from pandas [color=rgb(0, 0, 136) !important]import DataFramedata = pd.read_excel([color=rgb(0, 153, 0) !important]"example.xlsx", sheet_name=[color=rgb(0, 153, 0) !important]'Sheet1')[color=rgb(136, 0, 0) !important]# 删除gender列,需要指定axis为1,当删除行时,axis为0data = data.drop([color=rgb(0, 153, 0) !important]'gender', axis=[color=rgb(0, 102, 102) !important]1)[color=rgb(136, 0, 0) !important]# 删除第3,4行,这里下表以0开始,并且标题行不算在类data = data.drop([[color=rgb(0, 102, 102) !important]2, [color=rgb(0, 102, 102) !important]3], axis=[color=rgb(0, 102, 102) !important]0)[color=rgb(136, 0, 0) !important]# 保存DataFrame(data).to_excel([color=rgb(0, 153, 0) !important]'example.xlsx', sheet_name=[color=rgb(0, 153, 0) !important]'Sheet1', index=[color=rgb(0, 0, 136) !important]False, header=[color=rgb(0, 0, 136) !important]True)

这时候打开excel可以看见gender列和除标题行的第3,4行被删除了。

总结

pandas除了上述的基本功能以外,还有其它更高级的操作,想要进一步学习的小伙伴们可以去pandas网站进行学习。







欢迎光临 纳速健身 (https://www.nasue.com/) Powered by Discuz! X3.4