【问题标题】:how to read specific columns from csv file using python in google collab [duplicate]如何在google collab中使用python从csv文件中读取特定列[重复]
【发布时间】:2021-10-05 00:48:44
【问题描述】:
df = pd.read_csv(io.BytesIO(uploaded['data.csv']))



ParserError                               Traceback (most recent call last)
<ipython-input-22-a523fd2e8c08> in <module>()
      1 import io
      2 
----> 3 df = pd.read_csv(io.BytesIO(uploaded['data.csv']))
      4 
      5 

3 frames
/usr/local/lib/python3.7/dist-packages/pandas/io/parsers.py in read(self, nrows)
   2155     def read(self, nrows=None):
   2156         try:
-> 2157             data = self._reader.read(nrows)
   2158         except StopIteration:
   2159             if self._first_chunk:

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()

pandas/_libs/parsers.pyx in pandas._libs.parsers.raise_parser_error()

ParserError: Error tokenizing data. C error: Expected 3 fields in line 77, saw 5

【问题讨论】:

    标签: python pandas dataframe csv data-analysis


    【解决方案1】:
    import pandas as pd #import pandas
    fread =  pd.read_csv('filepath/filename.csv') #make fread as object
    freaddf = pd.DataFrame(fread, columns=['col1name', 'col2name', 'col3name'])
    print(freaddf.to_string())
    

    【讨论】:

    • 您能否添加一些更有用的解释或让您的 cmets 更有帮助?例如,#import pandas 评论实际上并没有告诉我代码没有告诉我的任何事情。
    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2015-02-07
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多