mercredi 7 novembre 2018

Itertools 2000 randomized Combinations Python Dataframe

I asked a question concerning combinations using the itertools on a dataframe old quesion. Here I wanted to calculate the sum between the Unit and Scen column with equal values for the same fact_date. This worked out like expected.

Now my question is instead doing a linear combination between all possible combinations, I wanted to create randomized combinations (keeping rand constant) and create e.g. 2000 combinations but can be any number of combinations and calculate the same values as my code is performing below but just randomized. It is clear that same combinations can appear.

The code from my old question looked like the following:

import pandas as pd
from itertools import combinations

df = pd.DataFrame({'fact_date': ['11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','11-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr','10-Apr'],
                   'Unit': ['a','a','b','b','c','c','d','d','a','a','b','b','c','c','d','d','e','e','f','f','g','g','h','h','e','e','f','f','g','g','h','h'],
                   'Town': ['Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town A','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B','Town B'],
                   'Scen': [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2],
                   'Value': [13,11,15,20,17,19,18,18,18,12,10,13,14,14,20,10,18,17,15,19,11,14,14,17,19,10,16,10,16,19,12,11],
                   'Country': ['USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA','USA']})


df_res = pd.DataFrame([list(name_g) + [val1+val2,'{},{}'.format(unit1,unit2)] 
                       for name_g, df_g in df.groupby(['fact_date','Country','Town','Scen']) 
                       for ((val1, unit1), (val2, unit2)) in combinations(df_g[['Value','Unit']].values,2)],
                       columns=['Combination','Country','Town','Scen','Value_Sum','Unit_Com'])


df_res1 = pd.DataFrame([list(name_g) + [-val1+val2,'{},{}'.format(unit1,unit2)] 
                       for name_g, df_g in df.groupby(['fact_date','Country','Town','Scen']) 
                       for ((val1, unit1), (val2, unit2)) in combinations(df_g[['Value','Unit']].values,2)],
                       columns=['Combination','Country','Town','Scen','Value_Sum','Unit_Com'])


df_res2 = pd.DataFrame([list(name_g) + [val1+val2,'{},{}'.format(unit1,unit2), '{},{}'.format(town1,town2)] 
                        for name_g, df_g in df.groupby(['fact_date','Country','Scen']) 
                        for ((val1, unit1, town1), (val2, unit2, town2)) in combinations(df_g[['Value','Unit','Town']].values,2)],
                        columns=['Combination','Country','Scen','Value_Sum','Unit_Com','Town'])

print(df_res2)




Aucun commentaire:

Enregistrer un commentaire