Pandas provides rich export functionalities, allowing you to export DataFrames to various common formats, including CSV, Excel, SQL databases, JSON, and more. This section details the usage methods and considerations for various export methods.
\\n\\n\\n\\n
Export to CSV
\\nCSV is the most universal data exchange format. Exporting can be affected by the environment's default encoding, so attention must be paid to Chinese encoding issues.
\\n\\nBasic Export
\\n\\nExample
\\nimport pandas as pd\\n\\n# Prepare test data\\ndf = pd.DataFrame({\\n "Name": ["Zhang San","Li Si","Wang Wu","Zhao Liu"],\\n "Age": [25,30,28,35],\\n "City": ["Beijing","Shanghai","Guangzhou","Shenzhen"],\\n "Salary": [12000,15000,11000,18000]\\n})\\n\\n# Most basic export (includes index)\\ndf.to_csv("output.csv")\\n\\n# Exclude index\\ndf.to_csv("output.csv", index=False)\\n\\n\\nChinese Encoding Handling
\\n\\nExample
\\nimport pandas as pd\\n\\ndf = pd.DataFrame({\\n "Name": ["Zhang San","Li Si","Wang Wu"],\\n "City": ["Beijing","Shanghai","Guangzhou"]\\n})\\n\\n# UTF-8 Encoding (recommended)\\ndf.to_csv("output_utf8.csv", encoding="utf-8")\\n\\n# UTF-8 with BOMοΌExcel Open without garbled text)\\ndf.to_csv("output_utf8_bom.csv", encoding="utf-8-sig")\\n\\n# GBK Encoding (suitable for legacy systems)\\ndf.to_csv("output_gbk.csv", encoding="gbk")\\n\\n# Verify encoding\\nimport os\\nprint("File size comparison:")\\nfor f in ["output_utf8.csv","output_utf8_bom.csv","output_gbk.csv"]:\\n print(f"{f}: {os.path.getsize(f)} bytes")\\n\\n\\nExport Options Details
\\n\\nExample
\\nimport pandas as pd\\n\\ndf = pd.DataFrame({\\n "Name": ["Zhang San","Li Si"],\\n "Age": [25,30]\\n})\\n\\n# Specify delimiter (default is comma)\\ndf.to_csv("output.tsv", sep="\\\\t") # TSV Format\\n\\n# Do not write header\\ndf.to_csv("output.csv", header=False)\\n\\n# Customize column names (When header=False HourοΌ\\ndf.to_csv("output.csv", header=False, columns=["Name","Age"])\\n\\n# Export onlySpecific column\\ndf.to_csv("output.csv", columns=) # Export only"Name"column\\n\\n# Missing value handling\\nimport numpy as np\\ndf_with_na = pd.DataFrame({\\n "A": [1,2, np.nan,4],\\n "B": ["a",None,"c","d"]\\n})\\ndf_with_na.to_csv("output.csv", na_rep="NULL") # Specify missing value representation\\n\\n# Floating-point precision\\ndf = pd.DataFrame({"value": [1.23456789,2.3456789]})\\ndf.to_csv("output.csv", float_format="%.2f") # Keep 2 decimal places\\n\\n\\n\\n\\n
Export to Excel
\\nThe Excel format is suitable for manual viewing and editing, but exporting large files can be slow.
\\n\\nInstall Dependencies
\\npip install openpyxl xlwt\\n\\n\\nBasic Export
\\n\\nExample
\\nimport pandas as pd\\n\\ndf = pd.DataFrame({\\n "Name": ["Zhang San","Li Si","Wang Wu"],\\n "Age": [25,30,28],\\n "City": ["Beijing","Shanghai","Guangzhou"],\\n "Salary": [12000,15000,11000]\\n})\\n\\n# Export as Excel (.xlsx FormatοΌRequires openpyxl)\\ndf.to_excel("output.xlsx", index=False)\\n\\n# Export as older version .xls FormatοΌRequires xlwt)\\ndf.to_excel("output.xls", index=False)\\n\\n\\nMulti-Sheet Export
\\n\\nExample
\\nimport pandas as pd\\n\\n# Create multiple DataFrames\\ndf1 = pd.DataFrame({"A": [1,2,3],"B": [4,5,6]})\\ndf2 = pd.DataFrame({"C": [7,8,9],"D": [10,11,12]})\\n\\n# Export to different sheets in the same Excel file\\nwith pd.ExcelWriter("output.xlsx", engine="openpyxl") as writer:\\n df1.to_excel(writer, sheet_name="Sheet1", index=False)\\n df2.to_excel(writer, sheet_name="Sheet2", index=False)\\n\\nprint("Multi Sheet Export successful")\\n\\n\\nFormatted Export
\\n\\nExample
\\nimport pandas as pd\\nfrom openpyxl import Workbook\\nfrom openpyxl.styles import Font, PatternFill, Alignment\\n\\n# Create DataFrame\\ndf = pd.DataFrame({\\n "Name": \\n\\n
\\n
YouTip