Skip to content

Commit 411e8ba

Browse files
authored
DOC: create folders in to_csv #24306 (#42250)
1 parent 439acc9 commit 411e8ba

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pandas/core/generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -3454,6 +3454,18 @@ def to_csv(
34543454
... archive_name='out.csv') # doctest: +SKIP
34553455
>>> df.to_csv('out.zip', index=False,
34563456
... compression=compression_opts) # doctest: +SKIP
3457+
3458+
To write a csv file to a new folder or nested folder you will first
3459+
need to create it using either Pathlib or os:
3460+
3461+
>>> from pathlib import Path
3462+
>>> filepath = Path('folder/subfolder/out.csv')
3463+
>>> filepath.parent.mkdir(parents=True, exist_ok=True)
3464+
>>> df.to_csv(filepath)
3465+
3466+
>>> import os
3467+
>>> os.makedirs('folder/subfolder', exist_ok=True)
3468+
>>> df.to_csv('folder/subfolder/out.csv')
34573469
"""
34583470
df = self if isinstance(self, ABCDataFrame) else self.to_frame()
34593471

0 commit comments

Comments
 (0)