How to use Python Improve your work efficiency , Let's share this with you today .
We often use pandas Read in read write excel file , I often meet a excel There are multiple in the file sheet file , This is the time , You need to read multiple at once sheet And do corresponding data analysis or data processing , Finally, write a new excel file ( There are also many sheet). This article introduces several sheet Read in the file and write it to the new file after processing the data excel file ( Multiple sheet) Operation process .
Python Exchange of learning Q Group :660193417####
The excel In file 4 individual sheet( Sometimes how many sheet Don't know ), Now read in all sheet form .
import pandas as pd
df=pd.read_excel(' input data 123.xlsx',sheet_name=None)# Read excel all sheet data
df
all sheet The contents of are read into df in .
View all sheet name
df.keys()
View a sheet
At every sheet Add a new column
for i in df.keys():
df[i][' month ']=df[i][' Fill in the date '].astype(str).apply(lambda x:int(x[5:7]))
df
Multiple copies of data are written into one excel file ( Multiple sheet)(https://jq.qq.com/?_wv=1027&k=Ap5XvyNN)
Now write the following four copies of data into a excel The difference of documents sheet in .
writer1 = pd.ExcelWriter(' Output data 0401.xlsx',engine='xlsxwriter')
for i in df.keys():
df[i].to_excel(writer1, sheet_name=i, index=False)
worksheet1 = writer1.sheets[i]
#worksheet1.set_column(1, 1, 150)# Set the width of the column
writer1.close()
This is the end of today's sharing , I hope this article can help you , If you like it, you can order a like .