Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple dataframes using multiple functions

I quite often write a function to return different dataframes based on the parameters I enter. Here's an example dataframe:

np.random.seed(1111)
df = pd.DataFrame({
'Category':np.random.choice( ['Group A','Group B','Group C','Group D'], 10000),
'Sub-Category':np.random.choice( ['X','Y','Z'], 10000),
'Sub-Category-2':np.random.choice( ['G','F','I'], 10000),
'Product':np.random.choice( ['Product 1','Product 2','Product 3'], 10000),
'Units_Sold':np.random.randint(1,100, size=(10000)),
'Dollars_Sold':np.random.randint(100,1000, size=10000),
'Customer':np.random.choice(pd.util.testing.rands_array(10,25,dtype='str'),10000),
'Date':np.random.choice( pd.date_range('1/1/2016','12/31/2018',  
                      freq='M'), 10000)})

I then created a function to perform sub-totals for me like this:

def some_fun(DF1, agg_column, myList=[], *args):
    y = pd.concat([
    DF1.assign(**{x:'[Total]' for x in myList[i:]})\
            .groupby(myList).agg(sumz = (agg_column,'sum')) for i in range(1,len(myList)+1)]).sort_index().unstack(0)
    return y

I then write out lists that I'll pass as arguments to the function:

list_one = [pd.Grouper(key='Date',freq='A'),'Category','Product']
list_two = [pd.Grouper(key='Date',freq='A'),'Category','Sub-Category','Sub-Category-2']
list_three = [pd.Grouper(key='Date',freq='A'),'Sub-Category','Product']

I then have to run each list through my function creating new dataframes:

df1 = some_fun(df,'Units_Sold',list_one)
df2 = some_fun(df,'Dollars_Sold',list_two)
df3 = some_fun(df,'Units_Sold',list_three)

I then use a function to write each of these dataframes to an Excel worksheet. This is just an example - I perform this same exercise 10+ times.

My question - is there a better way to perform this task than to write out df1, df2, df3 with the function information applied? Should I be looking at using a dictionary or some other data type to do this my pythonically with a function?

like image 963
keg5038 Avatar asked Jan 12 '20 03:01

keg5038


Video Answer


1 Answers

A dictionary would be my first choice:

variations = ([('Units Sold', list_one), ('Dollars_Sold',list_two), 
              ..., ('Title', some_list)])

df_variations = {}

for i, v in enumerate(variations):
     name = v[0]
     data = v[1]
     df_variations[i] = some_fun(df, name, data)

You might further consider setting the keys to unique / helpful titles for the variations, that goes beyond something like 'Units Sold', which isn't unique in your case.

like image 90
Thomas Kavanagh Avatar answered Oct 06 '22 23:10

Thomas Kavanagh