pandas dataframe读取csv,并在行尾添加/不包含逗号 [英] pandas dataframe read csv with rows that have/not have comma at the end

查看:71
本文介绍了pandas dataframe读取csv,并在行尾添加/不包含逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的示例文件如下:

1.50424e+09,164.84,164.94,163.4,164.07,09:30:00,1.50424e+12,eAAPL,1.38904e+07,0,22.45,2.64333e+07,847097,18.49,1.54
1.50459e+09,163.8,164.25,158.26,162.2,09:30:00,1.50459e+12,eAAPL,2.54615e+07,0,22.44,2.64646e+07,847097,18.49,1.54
1.50467e+09,162.71,162.99,160.52,162.01,09:30:00,1.50467e+12,eAAPL,1.67919e+07,0,22.67,2.61136e+07,837180,18.27,1.55
1.50485e+09,160.9,161.15,158.62,158.7,09:30:00,1.50485e+12,eAAPL,2.02651e+07,0,22.73,2.48843e+07,832945,18.18,1.56,
1.50511e+09,160.51,162.05,159.89,161.48,09:30:00,1.50511e+12,eAAPL,2.44948e+07,0,22.54,2.50082e+07,819360,17.88,1.59,
1.50476e+09,162.17,163.69,160.36,161.175,09:30:00,1.50476e+12,eAAPL,1.88933e+07,0,22.68,2.58778e+07,836302,18.25,1.56,

请滚动到最右边,您将看到前3行末没有逗号,但后3行有逗号.当我这样做

please scroll to the very right, you will see first 3 rows do not have comma at the end but the last 3 have comma. When i do

f = pd.read_csv("AAPL.csv", header=None)

它给出错误提示:

CParserError: Error tokenizing data. C error: Expected 15 fields in line 4, saw 16

我该如何解决?

推荐答案

您可以将 usecols np.arange(0,15)一起使用,而不必理会csv文件的底部三行:

You could use usecols with np.arange(0,15), ignoring that trailing column on the bottom three lines of your csv file:

from io import StringIO
file = StringIO("""1.50424e+09,164.84,164.94,163.4,164.07,09:30:00,1.50424e+12,eAAPL,1.38904e+07,0,22.45,2.64333e+07,847097,18.49,1.54
1.50459e+09,163.8,164.25,158.26,162.2,09:30:00,1.50459e+12,eAAPL,2.54615e+07,0,22.44,2.64646e+07,847097,18.49,1.54
1.50467e+09,162.71,162.99,160.52,162.01,09:30:00,1.50467e+12,eAAPL,1.67919e+07,0,22.67,2.61136e+07,837180,18.27,1.55
1.50485e+09,160.9,161.15,158.62,158.7,09:30:00,1.50485e+12,eAAPL,2.02651e+07,0,22.73,2.48843e+07,832945,18.18,1.56,
1.50511e+09,160.51,162.05,159.89,161.48,09:30:00,1.50511e+12,eAAPL,2.44948e+07,0,22.54,2.50082e+07,819360,17.88,1.59,
1.50476e+09,162.17,163.69,160.36,161.175,09:30:00,1.50476e+12,eAAPL,1.88933e+07,0,22.68,2.58778e+07,836302,18.25,1.56,""")

f = pd.read_csv(file, usecols=np.arange(0,15), header=None)

print(f.head())

输出:

             0       1       2       3       4         5             6   \
0  1.504240e+09  164.84  164.94  163.40  164.07  09:30:00  1.504240e+12   
1  1.504590e+09  163.80  164.25  158.26  162.20  09:30:00  1.504590e+12   
2  1.504670e+09  162.71  162.99  160.52  162.01  09:30:00  1.504670e+12   
3  1.504850e+09  160.90  161.15  158.62  158.70  09:30:00  1.504850e+12   
4  1.505110e+09  160.51  162.05  159.89  161.48  09:30:00  1.505110e+12   

      7           8   9      10          11      12     13    14  
0  eAAPL  13890400.0   0  22.45  26433300.0  847097  18.49  1.54  
1  eAAPL  25461500.0   0  22.44  26464600.0  847097  18.49  1.54  
2  eAAPL  16791900.0   0  22.67  26113600.0  837180  18.27  1.55  
3  eAAPL  20265100.0   0  22.73  24884300.0  832945  18.18  1.56  
4  eAAPL  24494800.0   0  22.54  25008200.0  819360  17.88  1.59  

这篇关于pandas dataframe读取csv,并在行尾添加/不包含逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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