Python如何提取csv数据并筛选指定条件数据详解
作者:PangAy 时间:2022-07-20 22:47:59
前言
本文主要介绍通过Python提取csv文件中数据,并对数据进行处理。
编译器:Anaconda3 语言:Python3
一、使用pandas和numpy库
通过pandas库可以从csv提取到数据,但是数据的格式是DateFrame 格式,因为我不懂怎么处理DateFrame 格式的数据,所以就使用numpy库,将提取的数据转化为数组格式,这样可以就可以通过对数组的处理方式处理csv文件的数据。写回csv文件的过程中,再转换为DateFrame即可。
二、使用步骤
1.引入库
代码如下(示例):
import pandas as pd
import numpy as np
2.读入数据
本文是通过路径读取的文件。如果想通过文件名读取,需要把文件放与程序放到同一个文件夹中
代码如下(示例):
file1=pd.read_csv(r'C:\Users\86150\Desktop\1_Part2 附加测试题\Part2 附加测试题\4. data\data【各省-竞品】.csv')
此时读出的数据是DateFrame格式,输出如下
3.转化格式
将DateFrame 格式的数据转化为数组
file1=np.array(file1)
此时得到输出如下
4.处理数据
我们将所有省份是‘黑龙江’的数据挑选出来,并放入新的数组中
data=[]
for item in file1:
sh= item[0]
if "黑龙江" ==sh:
# print(item)
data.append(item)
这样就把数据挑选出来并存放在data中
来源:https://blog.csdn.net/m0_46159136/article/details/113618135
标签:python,csv,筛选
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解读ASP.NET 5 & MVC6系列教程(2):初识项目
2023-06-28 02:10:47
![](https://img.aspxhome.com/file/2023/8/75528_0s.png)
ASP自动解压RAR文件代码
2007-11-06 13:29:00
Python字典和集合讲解
2021-03-05 22:31:14
![](https://img.aspxhome.com/file/2023/2/65552_0s.png)
通过视图修改数据时所应掌握的基本准则
2009-01-07 14:22:00
MySQL Basis 常用命令
2010-11-11 11:59:00
Python基于paramunittest模块实现excl参数化
2023-12-27 00:29:02
PHP设计模式(八)装饰器模式Decorator实例详解【结构型】
2023-11-24 05:59:31
![](https://img.aspxhome.com/file/2023/5/111355_0s.jpg)
python 自动化偷懒的四个实用操作
2023-11-19 08:49:48
![](https://img.aspxhome.com/file/2023/3/63943_0s.jpg)
discuz 跨域整合的记录文件
2023-07-23 14:17:27
Python heapq库案例详解
2022-12-27 12:26:52
![](https://img.aspxhome.com/file/2023/4/89444_0s.png)
Python 面向对象编程详解
2023-06-25 05:24:23
python webp图片格式转化的方法
2021-09-03 16:27:44
详解Python3的TFTP文件传输
2023-06-01 22:29:17
![](https://img.aspxhome.com/file/2023/6/87896_0s.png)
[图]关于网站开发中缓存 cache应用
2008-08-19 18:14:00
![](https://img.aspxhome.com/file/UploadPic/20088/19/200881918194691s.gif)
python 基于空间相似度的K-means轨迹聚类的实现
2022-10-24 07:29:02
![](https://img.aspxhome.com/file/2023/4/90294_0s.png)
教你利用PyTorch实现sin函数模拟
2021-06-23 18:17:25
Python 文档解析lxml库的使用详解
2022-01-24 10:42:38
python 实现围棋游戏(纯tkinter gui)
2023-11-23 16:13:04
![](https://img.aspxhome.com/file/2023/4/92074_0s.gif)
python之excel文件(.xls文件)处理方式
2021-08-22 15:58:14
![](https://img.aspxhome.com/file/2023/3/75973_0s.png)
Rs.Open参数说明
2008-05-12 22:43:00