使用 pandas 导入每行具有不同列数的csv [英] import csv with different number of columns per row using Pandas
本文介绍了使用 pandas 导入每行具有不同列数的csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Pandas或CSV模块将每行具有不同列数的CSV导入到Pandas DataFrame中的最佳方法是什么.
What is the best approach for importing a CSV that has a different number of columns for each row using Pandas or the CSV module into a Pandas DataFrame.
"H","BBB","D","Ajxxx Dxxxs"
"R","1","QH","DTR"," "," ","spxxt rixxls, raxxxd","1"
使用此代码:
import pandas as pd
data = pd.read_csv("smallsample.txt",header = None)
产生以下错误
Error tokenizing data. C error: Expected 4 fields in line 2, saw 8
推荐答案
在read_csv()中提供列名称列表应该可以解决问题.
Supplying a list of columns names in the read_csv() should do the trick.
ex:名称= ['a','b','c','d','e']
ex: names=['a', 'b', 'c', 'd', 'e']
https://github.com/pydata/pandas/issues/2981
如果您不想提供列名,请按照尼古拉斯的建议进行操作
if you don't want to supply column names then do what Nicholas suggested
这篇关于使用 pandas 导入每行具有不同列数的csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文