Экспорт кадра данных Pandas в файл Excel
Опубликовано: 27 Марта, 2022
Давайте посмотрим, как экспортировать Pandas DataFrame в файл Excel.
Алгоритм:
- Создайте DataFrame.
- Определите имя файла Excel.
- Вызовите функцию to_excel () с именем файла, чтобы экспортировать DataFrame.
Example 1:
Python3
# importing the moduleimport pandas as pd # creating the DataFramemarks_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 filefile_name = "MarksData.xlsx" # saving the excelmarks_data.to_excel(file_name)print("DataFrame is written to Excel File successfully.") |
Выход:
DataFrame успешно записан в файл Excel.
Файл Excel:

Example 2: We can also first use the ExcelWriter() method to save it.
Python3
# importing the moduleimport pandas as pd # creating the DataFramecars_data = pd.DataFrame({"Cars": ["BMW", "Audi", "Bugatti", "Porsche", "Volkswagen"], "MaxSpeed": [220, 230, 240, 210, 190], "Color": ["Black", "Red", "Blue", "Violet", "White"]}) # writing to Exceldatatoexcel = pd.ExcelWriter("CarsData1.xlsx") # write DataFrame to excelcars_data.to_excel(datatoexcel) # save the exceldatatoexcel.save()print("DataFrame is written to Excel File successfully.") |
Выход:
DataFrame успешно записан в файл Excel.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.