可变列失败的 pandas [英] pandas failing with variable columns

查看:59
本文介绍了可变列失败的 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件是这个

my file is this

    4 7 a a
    s g 6 8 0 d
    g 6 2 1 f 7 9 
    f g 3 
    1 2 4 6 8 9 0

我正在使用pandas将其保存为pandas对象的形式.但出现以下错误
pandas.parser.CParserError: Error tokenizing data. C error: Expected 6 fields in line 3, saw 8

I was using pandas to save it in form of pandas object. But I am getting the following error
pandas.parser.CParserError: Error tokenizing data. C error: Expected 6 fields in line 3, saw 8

我使用的代码是
file = pd.read_csv("a.txt",dtype = None,delimiter = " ")

The code I used was
file = pd.read_csv("a.txt",dtype = None,delimiter = " ")

任何人都可以提出一个想法来包含这样的文件吗?

Can anyone suggest an idea to include the file as such ?

推荐答案

这是一种方法.

In [50]: !type temp.csv
4,7,a,a
s,g,6,8,0,d
g,6,2,1,f,7,9
f,g,3
1,2,4,6,8,9,0

将csv读取到列表列表,然后转换为DataFrame.

Read the csv to list of lists and then convert to DataFrame.

In [51]: pd.DataFrame([line.strip().split(',') for line in open('temp.csv', 'r')])
Out[51]:
   0  1  2     3     4     5     6
0  4  7  a     a  None  None  None
1  s  g  6     8     0     d  None
2  g  6  2     1     f     7     9
3  f  g  3  None  None  None  None
4  1  2  4     6     8     9     0

这篇关于可变列失败的 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆