pandas数据筛选和csv操作的实现方法
作者:Fate0729 时间:2022-04-23 15:36:21
1. 数据筛选
a b c
0 0 2 4
1 6 8 10
2 12 14 16
3 18 20 22
4 24 26 28
5 30 32 34
6 36 38 40
7 42 44 46
8 48 50 52
9 54 56 58
(1)单条件筛选
df[df['a']>30]
# 如果想筛选a列的取值大于30的记录,但是之显示满足条件的b,c列的值可以这么写
df[['b','c']][df['a']>30]
# 使用isin函数根据特定值筛选记录。筛选a值等于30或者54的记录
df[df.a.isin([30, 54])]
(2)多条件筛选
可以使用&(并)与| (或)操作符或者特定的函数实现多条件筛选
# 使用&筛选a列的取值大于30,b列的取值大于40的记录
df[(df['a'] > 30) & (df['b'] > 40)]
(3)索引筛选
a. 切片操作
df[行索引,列索引]或df[[列名1,列名2]]
#使用切片操作选择特定的行
df[1:4]
#传入列名选择特定的列
df[['a','c']]
b. loc函数
当每列已有column name时,用 df [ ‘a' ] 就能选取出一整列数据。如果你知道column names 和index,且两者都很好输入,可以选择 .loc同时进行行列选择。
In [28]: df.loc[0,'c']
Out[28]: 4
In [29]: df.loc[1:4,['a','c']]
Out[29]:
a c
1 6 10
2 12 16
3 18 22
4 24 28
In [30]: df.loc[[1,3,5],['a','c']]
Out[30]:
a c
1 6 10
3 18 22
5 30 34
c. iloc函数
如果column name太长,输入不方便,或者index是一列时间序列,更不好输入,那就可以选择 .iloc了,该方法接受列名的index,iloc 使得我们可以对column使用slice(切片)的方法对数据进行选取。这边的 i 我觉得代表index,比较好记点。
In [35]: df.iloc[0,2]
Out[35]: 4
In [34]: df.iloc[1:4,[0,2]]
Out[34]:
a c
1 6 10
2 12 16
3 18 22
In [36]: df.iloc[[1,3,5],[0,2]]
Out[36]:
a c
1 6 10
3 18 22
5 30 34
In [38]: df.iloc[[1,3,5],0:2]
Out[38]:
a b
1 6 8
3 18 20
5 30 32
d. ix函数
ix的功能更加强大,参数既可以是索引,也可以是名称,相当于,loc和iloc的合体。需要注意的是在使用的时候需要统一,在行选择时同时出现索引和名称, 同样在同行选择时同时出现索引和名称。
df.ix[1:3,['a','b']]
Out[41]:
a b
1 6 8
2 12 14
3 18 20
In [42]: df.ix[[1,3,5],['a','b']]
Out[42]:
a b
1 6 8
3 18 20
5 30 32
In [45]: df.ix[[1,3,5],[0,2]]
Out[45]:
a c
1 6 10
3 18 22
5 30 34
e. at函数
根据指定行index及列label,快速定位DataFrame的元素,选择列时仅支持列名。
In [46]: df.at[3,'a']
Out[46]: 18
f. iat函数
与at的功能相同,只使用索引参数
In [49]: df.iat[3,0]
Out[49]: 18
2. csv操作
csv文件内容
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier X,001-1001,2341,$500.00 ,1/20/14
Supplier X,001-1001,2341,$500.00 ,1/20/14
Supplier X,001-1001,5467,$750.00 ,1/20/14
Supplier X,001-1001,5467,$750.00 ,1/20/14
Supplier Y,50-9501,7009,$250.00 ,1/30/14
Supplier Y,50-9501,7009,$250.00 ,1/30/14
Supplier Y,50-9505,6650,$125.00 ,2002/3/14
Supplier Y,50-9505,6650,$125.00 ,2002/3/14
Supplier Z,920-4803,3321,$615.00 ,2002/3/14
Supplier Z,920-4804,3321,$615.00 ,2002/10/14
Supplier Z,920-4805,3321,$615.00 ,2/17/14
Supplier Z,920-4806,3321,$615.00 ,2/24/14
(1)csv文件读写
关于read_csv函数中的参数说明参考博客:https://www.jb51.net/article/164445.htm
import pandas as pd
# 读写csv文件
df = pd.read_csv("supplier_data.csv")
df.to_csv("supplier_data_write.csv",index=None)
(2)筛选特定的行
#Supplier Nmae列中姓名包含'Z',或者Cost列中的值大于600
print(df[df["Supplier Name"].str.contains('Z')])
print(df[df['Cost'].str.strip('$').astype(float) > 600])
print(df.loc[(df["Supplier Name"].str.contains('Z'))|(df['Cost'].str.strip('$').astype(float) > 600.0),:])
#行中的值属于某个集合
li = [2341,6650]
print(df[df['Part Number'].isin(li)])
print(df.loc[df['Part Number'].astype(int).isin(li),:])
#行中的值匹配某个模式
print(df[df['Invoice Number'].str.startswith("001-")])
(3)选取特定的列
#选取特定的列
#列索引值,打印1,3列
print(df.iloc[:,1:4:2])
#列标题打印
print(df.loc[:,["Invoice Number", "Part Number"]])
#选取连续的行
print(df.loc[1:4,:])
来源:https://www.cnblogs.com/xiaobingqianrui/p/9996177.html
标签:pandas,数据筛选,csv操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python常用标准库之os模块功能
2022-03-03 03:49:59
透明数据加密(TDE)库的备份和还原
2012-07-21 14:44:08
如何动态在文档中加入<script></script>写入大段js?
2010-07-02 13:17:00
python opencv设置摄像头分辨率以及各个参数的方法
2021-03-05 00:23:34
微信小程序利用Canvas绘制图片和竖排文字详解
2023-08-20 02:35:43
![](https://img.aspxhome.com/file/2023/2/56022_0s.jpg)
python实现知乎高颜值图片爬取
2023-03-11 10:35:54
Pytorch模型的保存/复用/迁移实现代码
2023-12-12 10:37:43
python判断字符串或者集合是否为空的实例
2021-08-03 04:08:58
用python实现批量重命名文件的代码
2023-01-02 09:09:20
Python pandas DataFrame操作的实现代码
2021-07-24 00:49:43
Python requests timeout的设置
2022-01-27 01:20:40
Python安装并操作redis实现流程详解
2023-06-15 00:17:22
![](https://img.aspxhome.com/file/2023/7/100287_0s.png)
以PHP代码为实例详解RabbitMQ消息队列中间件的6种模式
2023-06-08 12:15:53
![](https://img.aspxhome.com/file/2023/2/55492_0s.png)
Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能示例
2023-07-18 20:11:01
![](https://img.aspxhome.com/file/2023/1/104871_0s.png)
注册表单的细节问题
2008-05-24 08:43:00
![](https://img.aspxhome.com/file/UploadPic/20085/24/200852485213503s.jpg)
Oracle SID存在解決方法
2009-06-19 17:34:00
Python可视化学习之matplotlib内置单颜色
2021-07-27 06:43:46
![](https://img.aspxhome.com/file/2023/9/89309_0s.jpg)
Jmeter并发执行Python 脚本的完整流程
2021-05-12 02:52:49
表单系列·出错字段排行榜
2008-07-01 12:57:00
Python中如何创建多线程?
2022-11-25 15:52:34