首页 > 解决方案 > 在 Google Colab 中将导入的 csv 文件作为 pandas DataFrame 的错误消息

问题描述

使用的代码是

import pandas as pd
url = 'https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv'
dataframe = pd.read_csv(url)
isinstance(dataframe, pd.DataFrame) # This lets me know the data is successfully imported as a DF.
dataframe.head()

但是最后一行是否会吐出一个意外错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    336             method = get_real_method(obj, self.print_method)
    337             if method is not None:
--> 338                 return method()
    339             return None
    340         else:

1 frames
/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py in to_html(self, buf, encoding, classes, notebook, border)
    977             if (i >= nlevels and self.fmt.index and self.multirow and
    978                     ilevels > 1):
--> 979                 # sum up rows to multirows
    980                 crow = self._format_multirow(crow, ilevels, i, strrows)
    981             buf.write(' & '.join(crow))

ModuleNotFoundError: No module named 'pandas.io.formats.html'

解决问题的建议 SO 问题处理错误:

ModuleNotFoundError: No module named 'pandas.io.formats.csvs'

这似乎不同。

标签: pythonpandasgoogle-colaboratory

解决方案


它对我有用。也许您需要更新您的python。但这里有一个替代解决方案。尝试这个:

import pandas as pd
import io
import requests

url = "https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv"
contents = requests.get(url).content
df = pd.read_csv(io.StringIO(contents.decode('utf-8')))
isinstance(df, pd.DataFrame) 
df.head()

我希望这会正常工作。:)


推荐阅读