python - 在 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'
这似乎不同。
解决方案
它对我有用。也许您需要更新您的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()
我希望这会正常工作。:)
推荐阅读
- camera - 统一的多个场景打破了一切
- json - 如何根据属性名称更新 JSONB 数组?
- nested - 无法在嵌套询问中访问自己的 [我自己]
- c# - 带有 SqlDataReader 的 IAsyncEnumerable 和取消令牌在取消时挂起
- c++ - 为什么我的程序不能识别结构中的变量?
- .net - 如何在 AutoCAD 启动时自动加载 .net DLL
- python - 如何在 Python 中修复此 TypeError?
- java - 带有 @OneToMany 和 @ManyToOne 映射的 Hibernate StackOverflowError
- react-native - 本机反应应用程序代码可以转换/回滚到本机移动代码(ios和android)吗?
- sql - SQL 统计推荐系统的好恶,协同过滤 User-Based