Search code examples
pythonlistpandasdataframestore

Python: Store multiple dataframe in list


I have a loop that read Excel sheets in a document. I want to store them all in a list:

  DF_list= list()

  for sheet in sheets:
     df= pd.read_excel(...)
     DF_list = DF_list.append(df)

If I type:

[df df df df]

it works.

Sorry I have a Matlab background and not very used to Python, but I like it. Thanks.


Solution

  • If you will use parameter sheet_name=None:

    dfs = pd.read_excel(..., sheet_name=None)
    

    it will return a dictionary of Dataframes:

    sheet_name : string, int, mixed list of strings/ints, or None, default 0
    
        Strings are used for sheet names, Integers are used in zero-indexed
        sheet positions.
    
        Lists of strings/integers are used to request multiple sheets.
    
        Specify None to get all sheets.
    
        str|int -> DataFrame is returned.
        list|None -> Dict of DataFrames is returned, with keys representing
        sheets.
    
        Available Cases
    
        * Defaults to 0 -> 1st sheet as a DataFrame
        * 1 -> 2nd sheet as a DataFrame
        * "Sheet1" -> 1st sheet as a DataFrame
        * [0,1,"Sheet5"] -> 1st, 2nd & 5th sheet as a dictionary of DataFrames
        * None -> All sheets as a dictionary of DataFrames