将大型 pandas 数据框下载为 CSV 文件

download large pandas data frame as CVS file

我正在尝试使用此 Django 参考资料[流式处理大型 CSV 文件][1]

[1]: https://docs.djangoproject.com/en/2.2/howto/outputting-csv/#streaming-large-csv-files 将 pandas 数据帧下载为 csv 文件。

它需要一台发电机。

# Generate a sequence of rows. The range is based on the maximum number of
# rows that can be handled by a single sheet in most spreadsheet
# applications.
rows = (["Row {}".format(idx), str(idx)] for idx in range(65536))

如果我有一个名为 my_df 的数据框(有 20 列和 10000 行)....我如何修改此逻辑以使用 my_df 而不是像示例中那样生成数字.

类似于:

response = HttpResponse(content_type='text/csv') # Format response as a CSV
filename = 'some_file_name.csv'
response['Content-Disposition'] = 'attachment; filename="' + filename + '"'# Name the CSV response
my_df.to_csv(response, encoding='utf-8', index=False)
return response