gpt4 book ai didi

python - 在 Google Colab 中导入 csv 文件作为 pandas DataFrame 时出现错误消息

转载 作者:行者123 更新时间:2023-12-04 04:09:47 24 4
gpt4 key购买 nike

使用的代码是

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'

suggested SO question to resolve the issue处理错误:

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()

我希望它能正常工作。 :)

关于python - 在 Google Colab 中导入 csv 文件作为 pandas DataFrame 时出现错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61942635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com