I want to save the 'df' to 'dataset' file.
There is my code The code file name is '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
I think I set the file path well, but I don't know where the mistake is.
And this is my file structure : enter image description here
Here is the error message :
(covid_project) C:\self_project\covid_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_project\covid_project\covid19_project\practice.py", line 13, in <module>
df.to_csv('dataset/covid_dataset.csv', encoding='utf-8')
File "C:\self_project\covid_project\lib\site-packages\pandas\core\generic.py", line 3563, in to_csv
return DataFrameRenderer(formatter).to_csv(
File "C:\self_project\covid_project\lib\site-packages\pandas\io\formats\format.py", line 1180, in to_csv
csv_formatter.save()
File "C:\self_project\covid_project\lib\site-packages\pandas\io\formats\csvs.py", line 241, in save
with get_handle(
File "C:\self_project\covid_project\lib\site-packages\pandas\io\common.py", line 697, in get_handle
check_parent_directory(str(handle))
File "C:\self_project\covid_project\lib\site-packages\pandas\io\common.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'
Here's the desired result:
- I want to save 'df' as 'covid_dataset.csv' file name in 'dataset' file.