下面提到了.txt格式的数据文件(df),其中一些记录缺少几个字段。缺少的字段应在相应列中保持为空白
例如,txt格式的数据文件是
1,name=Messi,car=ford,Price=234,Bike=Harley
2,name=Cavani,car=mazda,price=58,Bike=Ducatti
3,name=Dembele,car=toyota,Bike=Yamaha
4,name=kevin,car=Ford,price=989
5,name=Aguero,Bike=Ducatti
6,name=nadal,car=Ferrari,Bike=Harley
我希望文件以以下格式加载到Python: 具有相应列名的必需输出:
我想要的列名称为数字,卡纳姆,价格,比克内姆。我希望在数据框中填充各个列名称下的各个数据。各列字段下的空值应保持为空
由于格式问题,我无法发布输出图像或在此处键入输出。由于我是stackoverflow的新手,我没有足够的声誉来发布图片
请注意,我的数据集有一百万条记录
您可以将数据写入中间CSV。添加一些文件修改时间检查,只有在数据文本文件发生更改时才能进行转换
测试
专门处理这种非标准和非统一文件格式的高效库存在的可能性很小。因此,我将逐行手动解析这个文件到
list of dicts
中,其中缺少的键(列)可以由DataFrame()
构造函数处理代码:
结果:
相关问题 更多 >
编程相关推荐