Splitting dataframe into multiple dataframes

后端 未结 11 1375
南方客
南方客 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:31

    Can I ask why not just do it by slicing the data frame. Something like

    #create some data with Names column
    data = pd.DataFrame({'Names': ['Joe', 'John', 'Jasper', 'Jez'] *4, 'Ob1' : np.random.rand(16), 'Ob2' : np.random.rand(16)})
    
    #create unique list of names
    UniqueNames = data.Names.unique()
    
    #create a data frame dictionary to store your data frames
    DataFrameDict = {elem : pd.DataFrame for elem in UniqueNames}
    
    for key in DataFrameDict.keys():
        DataFrameDict[key] = data[:][data.Names == key]
    

    Hey presto you have a dictionary of data frames just as (I think) you want them. Need to access one? Just enter

    DataFrameDict['Joe']
    

    Hope that helps

提交回复
热议问题