简体   繁体   中英

How do I import my data into a table in an xlsx file using Pandas?

I am pretty new to Pandas and was wondering if there was a way that would let me take the data I want and put it in an xlsx file as a table.

This is what I have so far for my code:

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') 

If you want to create a new excel every time from a dataframe, pandas has a to_excel function which you should use. If you want to write multiple data frames to an excel this link is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM