我想保存'df'到'dataset'文件。
这是我的代码代码文件名为'practice.py'
import pandas as pd
df = pd.DataFrame({
"x1": [100000, 200000, 300000],
"x2": [400000, 500000, 600000],
"x3": [700000, 800000, 900000]},
index=[2019, 2021, 2020]
)
df.index.name = 'year' # set the index name
df.to_csv('dataset/covid_dataset.csv', encoding='utf-8') # save to file
我想我设置的文件路径是正确的,但是我不知道错误在哪里。
这是我的文件结构:输入图片描述
错误信息如下:
(covid_project) C:self_projectcovid_project>C:/self_project/covid_project/Scripts/python.exe c:/self_project/covid_project/covid19_project/practice.py
Traceback (most recent call last):
File "c:self_projectcovid_projectcovid19_projectpractice.py", line 13, in <module>
df.to_csv('dataset/covid_dataset.csv', encoding='utf-8')
File "C:self_projectcovid_projectlibsite-packagespandascoregeneric.py", line 3563, in to_csv
return DataFrameRenderer(formatter).to_csv(
File "C:self_projectcovid_projectlibsite-packagespandasioformatsformat.py", line 1180, in to_csv
csv_formatter.save()
File "C:self_projectcovid_projectlibsite-packagespandasioformatscsvs.py", line 241, in save
with get_handle(
File "C:self_projectcovid_projectlibsite-packagespandasiocommon.py", line 697, in get_handle
check_parent_directory(str(handle))
File "C:self_projectcovid_projectlibsite-packagespandasiocommon.py", line 571, in check_parent_directory
raise OSError(fr"Cannot save file into a non-existent directory: '{parent}'")
OSError: Cannot save file into a non-existent directory: 'dataset'
下面是期望的结果:
- 我想将'df'保存为'covid_dataset.csv'文件名在'dataset'文件中。
在我的机器上运行windows 11python 3.8
尝试"。/数据/covid_dataset.csv">