Search code examples
pythonpandaslistdataframesplit

split dataframe into multiple dataframes using loop and lists


I'm attempting to create Dataframe using list. I have 2 List, I'm splitting list into multiple lists. using that multiple lists I'm creating dataframe and now I want to split that created dataframe.

below is the code of creating dataframe using list:

origin_list = ['60.17202,24.91805','51.13747,1.33148','55.65348,22.94213','61.17202,24.91805','62.13747,1.33148','63.65348,22.94213']
Destination_list = ['51.07906,12.13216','52.96035,1.905025','53.05306,16.13416','54.07906,3.13216','55.03406,12.13216','56.07906,12.13216','57.96035,1.905025','58.05306,16.13416','59.07906,3.13216','60.03406,12.13216']

# Code for splitting list into multiple lists 
origin_li = [origin_list[i:i + 3] for i in range(0, len(origin_list), 3)]
destination_li = [Destination_list[i:i + 4] for i in range(0, len(Destination_list), 4)]

# Output of above 2 lines
# origing_li = [['60.17202,24.91805', '51.13747,1.33148', '55.65348,22.94213'], ['61.17202,24.91805', '62.13747,1.33148', '63.65348,22.94213']]
# destination_li = [['51.07906,12.13216', '52.96035,1.905025', '53.05306,16.13416', '54.07906,3.13216'], ['55.03406,12.13216', '56.07906,12.13216', '57.96035,1.905025', '58.05306,16.13416'], ['59.07906,3.13216', '60.03406,12.13216']]

df1 = pd.DataFrame()
# loop for every list
for i in origin_li:
    print(len(i))
    for j in destination_li:
        sub_df = pd.DataFrame(list(itertools.product(i,j)))
        df1 = pd.concat([df1,sub_df])
print(df1)

by running above code I'm getting an output like: DataFrame

Now I want to split that output_dataframe by origin_li. For eg. Multiple Dataframes

How do I split dataframe into multiple dataframes?


Solution

  • You can use groupby to create your dataframes:

    dfs = dict(list(df1.groupby(np.arange(len(df1)) // 4)))
    

    Output:

    >>> dfs[1]
                      0                  1
    4  51.13747,1.33148  51.07906,12.13216
    5  51.13747,1.33148  52.96035,1.905025
    6  51.13747,1.33148  53.05306,16.13416
    7  51.13747,1.33148   54.07906,3.13216
    
    >>> dfs[5]
                        0                  1
    8   55.65348,22.94213  55.03406,12.13216
    9   55.65348,22.94213  56.07906,12.13216
    10  55.65348,22.94213  57.96035,1.905025
    11  55.65348,22.94213  58.05306,16.13416