首页 > 解决方案 > 如何使用 Pandas 将我的数据导入到 xlsx 文件中的表中?

问题描述

我对 Pandas 很陌生,想知道是否有一种方法可以让我获取我想要的数据并将其作为表格放入 xlsx 文件中。

这是我到目前为止的代码:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import os
from openpyxl import load_workbook

#defining the excel file and the sheet(s) and printing them
excel_file_1 = 'Incident Report.xls' 
df_first_shift = pd.read_excel(r'C:\Users\M\Downloads\Incident Report.xls') 
print(df_first_shift) 

#combining all the data from the sheets into one file
df_all = pd.concat([df_first_shift]) 

#Creating the .xlsx file 
df_all.to_excel(r'C:\Users\M\OneDrive\Test\Incident_Report.xlsx') 

#Deleting a row
wb = load_workbook(r'C:\Users\M\OneDrive\Test\Incident_Report.xlsx') 
ws = wb['Sheet1']  
ws.delete_cols(1) 
wb.save(r'C:\Users\M\OneDrive\Test\Incident_Report.xlsx') 

标签: pythonexcelpandas

解决方案


如果您想每次从数据框中创建一个新的 excel,pandas 有一个to_excel函数,您应该使用它。如果您想将多个数据框写入 Excel,此链接很有帮助。


推荐阅读