本文主要为大家分享一篇Python读csv文件去掉一列后再写入新的文件实例,具有很的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家更好掌握Python
用了两种.方式解决该问题,都是网上现有的解决方案。
场景说明:
有一个数据文件,以文本方式保存,现在有三列user_id,plan_id,mobile_id。目标是得到新文件只有mobile_id,plan_id。
解决方案
方案一:用python的打开文件写文件的方式直接撸一遍数据,for循环内处理数据并写入到新文件。
代码如下:
def readwrite1( input_file,output_file): f = open(input_file, 'r') out = open(output_file,'w') print (f) for line in f.readlines(): a = line.split(",") x=a[0] + "," + a[1]+"\n" out.writelines(x) f.close() out.close()
方案二:用 pandas 读数据到 DataFrame 再做数据分割,直接用 DataFrame 的写入功能写到新文件
代码如下:
def readwrite2(input_file,output_file): date_1=pd.read_csv(input_file,header=0,sep=',') date_1[['mobile', 'plan_id']].to_csv(output_file, sep=',', header=True,index=False)
从代码上看,pandas逻辑更清晰。
下面看下执行的效率吧!
def getRunTimes( fun ,input_file,output_file): begin_time=int(round(time.time() * 1000)) fun(input_file,output_file) end_time=int(round(time.time() * 1000)) print("读写运行时间:",(end_time-begin_time),"ms") getRunTimes(readwrite1,input_file,output_file) #直接撸数据 getRunTimes(readwrite2,input_file,output_file1) #使用dataframe读写数据
读写运行时间: 976 ms
读写运行时间: 777 ms
input_file 大概有27万的数据,dataframe的效率比for循环效率还是要快一点的,如果数据量更大些,效果是否更明显呢?
下面试下增加input_file记录的数量试试,有如下结果
input_file | readwrite1 | readwrite2 |
27W | 976 | 777 |
55W | 1989 | 1509 |
110W | 4312 | 3158 |
从上面测试结果来看,dataframe的效率提高大约30%左右。
相关推荐:
以上是Python读csv文件去掉一列后再写入新的文件技术教程的详细内容。更多信息请关注PHP中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器