Splitting dataframe into multiple dataframes

后端 未结 11 1287
南方客
南方客 2020-11-22 01:16

I have a very large dataframe (around 1 million rows) with data from an experiment (60 respondents).

I would like to split the dataframe into 60 dataframes (a datafra

11条回答
  •  孤城傲影
    2020-11-22 01:33

    I had similar problem. I had a time series of daily sales for 10 different stores and 50 different items. I needed to split the original dataframe in 500 dataframes (10stores*50stores) to apply Machine Learning models to each of them and I couldn't do it manually.

    This is the head of the dataframe:

    I have created two lists; one for the names of dataframes and one for the couple of array [item_number, store_number].

        list=[]
        for i in range(1,len(items)*len(stores)+1):
        global list
        list.append('df'+str(i))
    
        list_couple_s_i =[]
        for item in items:
              for store in stores:
                      global list_couple_s_i
                      list_couple_s_i.append([item,store])
    

    And once the two lists are ready you can loop on them to create the dataframes you want:

             for name, it_st in zip(list,list_couple_s_i):
                       globals()[name] = df.where((df['item']==it_st[0]) & 
                                                    (df['store']==(it_st[1])))
                       globals()[name].dropna(inplace=True)
    

    In this way I have created 500 dataframes.

    Hope this will be helpful!

提交回复
热议问题