简体   繁体   中英

Pandas: Save multiple sheets into separate dataframes

I have an Excel file with multiple sheets, and I'd like to save each of sheets into a separate dataframe using a loop.

countries = ['Thailand', 'China']
for country in countries:
    (country + '_data') = pd.read_excel(path, sheet_name = country)

However I am getting an error message: SyntaxError: can't assign to operator

I know the assignment is incorrect, but how to build a correct loop that will produce separate dataframes for each of the sheets?

A better method would be

  1. using the built-in pd.ExcelFile method
  2. using a dictionary to hold your sheets in a separate data frames.

for example:

xl = pd.ExcelFile(file)
print(xl.sheet_names)
['China','Japan','Pakistan'...] 

then you can assign them to a dict

d = {} # your dict.
for sheet in xl.sheet_names:
    d[f'{sheet}']= pd.read_excel(xl,sheet_name=sheet)

then you can call your individual dfs

d['China'] 

I'm guessing this is the kind of thing you're looking for since you asked for a loop. Keep in mind that using a loop makes it harder to assign the df to a variable.

Also note that read_excel reads the first sheet as 0

import pandas as pd

num_sheets = 3
sheets = dict()

for i in range(num_sheets):
    sheets[i] = pd.read_excel('./test.xlsx', sheet_name = i)

Using a dictionary is the only easy way to include a loop on the title.

If you don't need a loop you could simply use the read_excel part specifying the sheet name or number.

import pandas as pd

thailand_data = pd.read_excel('./test.xlsx', sheet_name = 'Thailand')

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