Last active
November 30, 2023 17:29
-
-
Save jonperron/733c3ead188f72f0a8a6f39e3d89295d to your computer and use it in GitHub Desktop.
Serve pandas dataframe as csv with Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponse | |
import pandas as pd | |
def foo(): | |
results = pd.Dataframe() | |
response = HttpResponse(content_type='text/csv') | |
response['Content-Disposition'] = 'attachment; filename=filename.csv' | |
results.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",") | |
return response |
Thank you for posting that!
Thank you!!
Thank you
Great post! Thanks. By the way, should the results.to_csv
in the 10th line be your_dataframe.to_csv
?
Yes indeed ! Thank you for spotting my typo
Thank you for sharing!
Thanks a lot, this saved me a lot of time!
genial!
Thanks man, saved time
Amazing!
Thank you so much! - it's brilliant!
Thank you so much -- found it after a long struggle but really helpful.
between
results.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",")
can be just
results.to_csv(path_or_buf=response)
if user do not want to go much in details of csv format and getting desired results.
perfect
thank you so much !
nice
Perfect
Thank you
That's good, worked in just 1st try.. Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, you saved me a lot of time!