{"id":23,"date":"2017-06-02T14:13:33","date_gmt":"2017-06-02T19:13:33","guid":{"rendered":"http:\/\/1uslchriston.ad.here.com:8080\/blog\/?p=23"},"modified":"2019-07-02T14:28:51","modified_gmt":"2019-07-02T19:28:51","slug":"pandas-how-to-export-a-dataframe-to-csv","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/06\/02\/pandas-how-to-export-a-dataframe-to-csv\/","title":{"rendered":"Pandas: How to export a DataFrame to .csv"},"content":{"rendered":"<p>Here is a function I wrote that will export an entire DataFrame to csv. It uses the Pandas function <code>to_csv()<\/code>. Just feed it the name of the DataFrame and the name you want for the .csv file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def dataframe_to_csv(filename, DataFrame):\n    \"\"\"Export entire DataFrame to csv.\"\"\"\n    output = DataFrame\n    output.to_csv(filename, index=True)<\/pre>\n<p>The filename can be a full file path (or if you have your local directory defined in your Jupyter Notebook using \u2018cd\u2019, you can just use the file name itself). The function can be called like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">dataframe_to_csv('Exported_dataframe.csv', batch_df)\n# or\ndataframe_to_csv('C:\\Users\\username\\data\\Exported_dataframe.csv', batch_df)<\/pre>\n<p>Alternatively, you may want to just export one or more columns from the DataFrame. Here is a function I wrote for that:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def partial_dataframe_to_csv(filename, DataFrame, columns):\n    \"\"\"Export a portion of a DataFrame to csv.\"\"\"\n    output = DataFrame\n    # Write only the fields found in the columns list\n    output = output[columns]\n    output.to_csv(filename, index=False)<\/pre>\n<p>&#8216;columns&#8217; is a list that contains the column header names that you want exported. The function can be called like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">columns_to_export = [\"isPlace\", \"isOpen\"]\npartial_dataframe_to_csv(\"DataFrame_portion.csv\", my_dataframe, columns_to_export)<\/pre>\n<p>For more information see: <a href=\"http:\/\/pandas.pydata.org\/pandas-docs\/stable\/generated\/pandas.DataFrame.to_csv.html?highlight=to_csv#pandas.DataFrame.to_csv\" target=\"_blank\" rel=\"noopener noreferrer\">pandas.DataFrame.to_csv<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a function I wrote that will export an entire DataFrame to csv. It uses the Pandas function to_csv(). Just feed it the name of the DataFrame and the name you want for the .csv file. def dataframe_to_csv(filename, DataFrame): &#8220;&#8221;&#8221;Export entire DataFrame to csv.&#8221;&#8221;&#8221; output = DataFrame output.to_csv(filename, index=True) The filename can be a &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/06\/02\/pandas-how-to-export-a-dataframe-to-csv\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Pandas: How to export a DataFrame to .csv<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[8,6,3,4,204],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-pandas","tag-csv","tag-dataframe","tag-pandas","tag-python","tag-to_csv"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":8,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":2756,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/23\/revisions\/2756"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}