Pandas has multiple worksheets and workbooks
Pandas reads multiple worksheets
import pandas as pd
df=pd.read_excel('the attachment 1. XLSX',sheet_name=None)
for sheetname, data in df.items():
print(sheetname)
print(data)
Copy the code
Be sure to set sheet_name=None
Data read after setting returns results by worksheet name: dictionary of data
Do not set the first worksheet content to be read by default
Read a set of worksheets
import pandas as pd
df=pd.read_excel('the attachment 1. XLSX',sheet_name=['Urban epidemic'.'City-province Cross Reference Table'])
for sheetname, data in df.items():
print(sheetname)
print(data)
Copy the code
Numerical indexes can also be used
import pandas as pd
df=pd.read_excel('the attachment 1. XLSX',sheet_name=[0.1])
for sheetname, data in df.items():
print(sheetname)
print(data)
Copy the code
Connection data
Concat functions to concatenate data
Vertical stack parameter Axis =0
Parallel connection parameter Axis =1
Multiple worksheet data connections
import pandas as pd
df=pd.read_excel('the attachment 1. XLSX',sheet_name=None)
da=[]
for sheetname, data in df.items():
da.append(data)
alldata=pd.concat(da,axis=1)
print(alldata)
Copy the code
Multiple workbook data links
Os.getcwd () returns the current directory. If you need to specify a directory, change os.getcwd() to a directoryCopy the code
import pandas as pd
import os
workbooks=[]
Get the name of the workbook in the current directory
for root, dirs, files in os.walk(os.getcwd()):
for file in files:
if os.path.splitext(file)[1] = ='.xlsx':
workbooks.append(file)
print(workbooks)
da=[]
for workbook in workbooks:
df=pd.read_excel(workbook,sheet_name=None)
for sheetname, data in df.items():
da.append(data)
alldata=pd.concat(da,axis=1)
print(alldata)
Copy the code