我正在尝试将 pandas DataFrame 保存为
csv
到我在 Databricks 工作区中创建的目录。
import pandas as pd
df.to_csv("data.csv", index=False)
OSError Traceback (most recent call last)
File <command-1050805454984878>:2
1 # Save CSV to Databricks filesystem
----> 2 df.to_csv(str(os.getcwd()) + "data.csv", index=False)
File /databricks/python/lib/python3.9/site-packages/pandas/core/generic.py:3551, in NDFrame.to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal, errors, storage_options)
3540 df = self if isinstance(self, ABCDataFrame) else self.to_frame()
3542 formatter = DataFrameFormatter(
3543 frame=df,
3544 header=header,
(...)
3548 decimal=decimal,
3549 )
-> 3551 return DataFrameRenderer(formatter).to_csv(
3552 path_or_buf,
3553 line_terminator=line_terminator,
3554 sep=sep,
3555 encoding=encoding,
3556 errors=errors,
3557 compression=compression,
3558 quoting=quoting,
3559 columns=columns,
3560 index_label=index_label,
3561 mode=mode,
3562 chunksize=chunksize,
3563 quotechar=quotechar,
3564 date_format=date_format,
3565 doublequote=doublequote,
3566 escapechar=escapechar,
3567 storage_options=storage_options,
3568 )
File /databricks/python/lib/python3.9/site-packages/pandas/io/formats/format.py:1180, in DataFrameRenderer.to_csv(self, path_or_buf, encoding, sep, columns, index_label, mode, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, errors, storage_options)
1159 created_buffer = False
1161 csv_formatter = CSVFormatter(
1162 path_or_buf=path_or_buf,
1163 line_terminator=line_terminator,
(...)
1178 formatter=self.fmt,
1179 )
-> 1180 csv_formatter.save()
1182 if created_buffer:
1183 assert isinstance(path_or_buf, StringIO)
File /databricks/python/lib/python3.9/site-packages/pandas/io/formats/csvs.py:261, in CSVFormatter.save(self)
241 with get_handle(
242 self.filepath_or_buffer,
243 self.mode,
(...)
249
250 # Note: self.encoding is irrelevant here
251 self.writer = csvlib.writer(
252 handles.handle,
253 lineterminator=self.line_terminator,
(...)
258 quotechar=self.quotechar,
259 )
--> 261 self._save()
File /databricks/python/lib/python3.9/site-packages/pandas/io/formats/csvs.py:266, in CSVFormatter._save(self)
264 if self._need_to_save_header:
265 self._save_header()
--> 266 self._save_body()
File /databricks/python/lib/python3.9/site-packages/pandas/io/formats/csvs.py:304, in CSVFormatter._save_body(self)
302 if start_i >= end_i:
303 break
--> 304 self._save_chunk(start_i, end_i)
File /databricks/python/lib/python3.9/site-packages/pandas/io/formats/csvs.py:315, in CSVFormatter._save_chunk(self, start_i, end_i)
312 data = [res.iget_values(i) for i in range(len(res.items))]
314 ix = self.data_index[slicer]._format_native_types(**self._number_format)
--> 315 libwriters.write_csv_rows(
316 data,
317 ix,
318 self.nlevels,
319 self.cols,
320 self.writer,
321 )
File /databricks/python/lib/python3.9/site-packages/pandas/_libs/writers.pyx:72, in pandas._libs.writers.write_csv_rows()
OSError: [Errno 22] Invalid argument
我希望将其保存为
csv
或 .pickle
文件。我还尝试指定目录的完整绝对路径。