程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Efficiency efficiency! How to read and write multiple sheet files using Python

編輯:Python

Preface

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 .

Read the file (https://jq.qq.com/?_wv=1027&k=Ap5XvyNN)

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

Last

This is the end of today's sharing , I hope this article can help you , If you like it, you can order a like .

I'm a panda , I'll see you in the next chapter (*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved