【问题标题】:Accessing Github data in Jupyter Books在 Jupyter Books 中访问 Github 数据
【发布时间】:2021-08-03 08:11:46
【问题描述】:

当我尝试访问 Jupyter Books 中的 csv 文件时出现标记化错误。看了一些回复,但似乎没有任何帮助。任何帮助,将不胜感激。谢谢。

url = "https://github.com/Kallikrates/bde_at2/blob/3875fd9b03b02b2772129acf2d8d83619971b2eb/2016Census_G01_NSW_LGA.csv"
insert_df = pd.read_csv(url, header=0, sep=',', quotechar='"')
insert_df.head()

错误:

---------------------------------------------------------------------------

ParserError                               Traceback (most recent call last)

<ipython-input-21-21c294baaa45> in <module>()
      1 url = "https://github.com/Kallikrates/bde_at2/blob/3875fd9b03b02b2772129acf2d8d83619971b2eb/2016Census_G01_NSW_LGA.csv"
----> 2 insert_df = pd.read_csv(url, header=0, sep=',', quotechar='"')
      3 insert_df.head()

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 1 fields in line 79, saw 2

【问题讨论】:

  • 尝试insert_df = pd.read_html(url),结果将是列表,因此您的数据集将是insert_df[0].head()

标签: python pandas csv github


【解决方案1】:

两个选项:

第一个:读为html

url = "https://github.com/Kallikrates/bde_at2/blob/3875fd9b03b02b2772129acf2d8d83619971b2eb/2016Census_G01_NSW_LGA.csv"
insert_df = pd.read_html(url)
insert_df[0].head(2)

第二次读取为 raw,观察其中的 URL,“raw”。

url="https://raw.githubusercontent.com/Kallikrates/bde_at2/3875fd9b03b02b2772129acf2d8d83619971b2eb/2016Census_G01_NSW_LGA.csv"
insert_df_raw = pd.read_csv(url, header=0, sep=',', quotechar='"')
insert_df_raw.head(2)

输出:

【讨论】:

  • 太棒了。谢谢@simpleApp!
猜你喜欢
  • 2019-01-28
  • 2017-05-31
  • 2015-04-30
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 2020-02-23
  • 2018-03-08
相关资源
最近更新 更多