Python读取多个文件夹下的csv文件并进行数据筛选

Python读取多个文件夹下的csv文件并进行数据筛选

最近做毕业设计,要处理武汉市多日的出租车GPS轨迹数据,正在学习Python进行数据分析与挖掘,欢迎学习交流~

数据为武汉市出租车轨迹数据
格式:
1.车辆ID, 2.GPS时间, 3.经度, 4.纬度, 5.方向(度), 6.速度(km/h), 7.空/重载

# 导入需要用的包
import pandas as pd
import numpy as np
from pathlib import Path


SaveFile_Path = "D:\Wuhan_GPS_txt--[20140501-0507]" # 拼接后要保存的文件路径
SaveFile_Name = 'WuHan_GPS_Data' # 合并后要保存的文件名

path="D:\Wuhan_GPS_txt--[20140501-0507]"  #要拼接的文件夹及其完整路径,注意不要包含中文
p=Path(path)
for file_name in p.rglob('*.txt'):  # 通过pathlib.Path.rglob()方法可以直接遍历汇总所有文件的绝对路径,直接用pandas读取即可
    file = pd.read_csv(file_name, header=None, names=['车辆ID', 'GPS时间', '经度', '纬度', '方向', '速度', '载客情况'],
                                dtype={
     '车辆ID': str, 'GPS时间': str, '经度': str, '纬度': str, '方向': str, '速度': str, '载客情况': str})

    Var = range(5, 100)    #数据去噪音 筛选出速度在5`100之间的数据
    data=(file.loc[file['速度'].astype(int).isin(Var), :])
    data.to_csv(SaveFile_Path + '\\' + SaveFile_Name, encoding="utf_8_sig", index=False, header=False, mode='a+')
   #保存至file文件中,index=False表示文件中不添加索引,header=False表示不添加列名,mode='a+'表示在已有数据基础上添加新数据,并不覆盖已有数据

在数据筛选的时候:


speed = [5,10]
print(file.loc[file['速度'].astype(int).isin(li),:])

这种情况只能筛选出速度为5和100的数据

  Var = range(5, 100) 
  print(file.loc[file['速度'].astype(int).isin(li),:])

筛选出[5,100]的速度

参考资料:
1.用Python遍历文件夹下的所有文件并进行数据处理(Pathlib简介)
https://blog.csdn.net/sinat_33264502/article/details/109071956?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242
2.python之pandas数据筛选和csv操作
https://www.cnblogs.com/xiaobingqianrui/p/9996177.html

你可能感兴趣的:(Python,python,数据分析,大数据)