📜  将 Pandas DataFrame 导出到 Excel 文件

📅  最后修改于: 2022-05-13 01:54:54.786000             🧑  作者: Mango

将 Pandas DataFrame 导出到 Excel 文件

让我们看看如何将 Pandas DataFrame 导出到 Excel 文件。

算法:

  1. 创建数据框。
  2. 确定 Excel 文件的名称。
  3. 使用文件名调用to_excel()函数以导出 DataFrame。

示例 1:

Python3
# importing the module
import pandas as pd
  
# creating the DataFrame
marks_data = pd.DataFrame({'ID': {0: 23, 1: 43, 2: 12,
                                 3: 13, 4: 67, 5: 89,
                                 6: 90, 7: 56, 8: 34},
                          'Name': {0: 'Ram', 1: 'Deep',
                                   2: 'Yash', 3: 'Aman',
                                   4: 'Arjun', 5: 'Aditya',
                                   6: 'Divya', 7: 'Chalsea',
                                   8: 'Akash' },
                          'Marks': {0: 89, 1: 97, 2: 45, 3: 78,
                                    4: 56, 5: 76, 6: 100, 7: 87,
                                    8: 81},
                          'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',
                                    4: 'E', 5: 'C', 6: 'A', 7: 'B',
                                    8: 'B'}})
  
# determining the name of the file
file_name = 'MarksData.xlsx'
  
# saving the excel
marks_data.to_excel(file_name)
print('DataFrame is written to Excel File successfully.')


Python3
# importing the module
import pandas as pd
  
# creating the DataFrame
cars_data = pd.DataFrame({'Cars': ['BMW', 'Audi', 'Bugatti', 
                                   'Porsche', 'Volkswagen'],
                          'MaxSpeed': [220, 230, 240, 210, 190],
                          'Color': ['Black', 'Red', 'Blue', 
                                    'Violet', 'White']})
  
# writing to Excel
datatoexcel = pd.ExcelWriter('CarsData1.xlsx')
  
# write DataFrame to excel
cars_data.to_excel(datatoexcel)
  
# save the excel
datatoexcel.save()
print('DataFrame is written to Excel File successfully.')


输出:

DataFrame is written to Excel File successfully.

Excel文件是:

例2:我们也可以先用ExcelWriter()方法保存。

Python3

# importing the module
import pandas as pd
  
# creating the DataFrame
cars_data = pd.DataFrame({'Cars': ['BMW', 'Audi', 'Bugatti', 
                                   'Porsche', 'Volkswagen'],
                          'MaxSpeed': [220, 230, 240, 210, 190],
                          'Color': ['Black', 'Red', 'Blue', 
                                    'Violet', 'White']})
  
# writing to Excel
datatoexcel = pd.ExcelWriter('CarsData1.xlsx')
  
# write DataFrame to excel
cars_data.to_excel(datatoexcel)
  
# save the excel
datatoexcel.save()
print('DataFrame is written to Excel File successfully.')

输出:

DataFrame is written to Excel File successfully.