在Python中使用本地数据Seaborn返回文件不存在

2024-09-30 02:32:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图利用Seaborn来创造一个形象。在

以下是我目前所掌握的情况:

import os.path
directory = os.path.dirname(os.path.abspath(__file__)) 
import pandas as pd
import seaborn as sns
sns.set(style="whitegrid", color_codes=True)
tel = pd.read_csv('nyc.csv')
nyctel = sns.load_dataset(tel)
sns.stripplot(x="installation_id", y="mounting", hue="mounting", data=nyctel)

load\u数据集的官方文档完全没有用,所以我发现有人已经问过它在这里是如何工作的:https://stackoverflow.com/a/30337377/6110631

我按照答案中列出的格式导入pandas,这样我就可以使用本地文件(保存在同一个文件夹中)。但是,当我运行程序时

^{pr2}$

如果我用绝对路径

IOError: ('http protocol error', 0, 'got a bad status line', None) 

问题似乎出在这条线上:

nyctel = sns.load_dataset(tel)

因为如果我省略这一行和它下面的那一行,并在下面添加print telpd.read_csv文件然后程序工作并打印出文件的内容。不知何故load_dataset不允许我使用该文件!在

我使用的代码与上面链接的答案完全相同。为什么这对本地文件无效?在


Tags: 文件csvpathimportpandasreadosas
1条回答
网友
1楼 · 发布于 2024-09-30 02:32:09

我是通过手机发布的,所以没有经过测试:

import pandas as pd
import seaborn as sns
import os.path

directory = os.path.dirname(os.path.abspath(__file__))
filename = 'nyc.csv'
file_path = os.path.join(directory, filename)
tel = pd.read_csv(file_path)

sns.set(style="whitegrid", color_codes=True)
nyctel = sns.load_dataset(tel)

sns.stripplot(x="installation_id", y="mounting", hue="mounting", data=nyctel)

相关问题 更多 >

    热门问题