0

I define my code to find correlation between 'day' and 'sales' each store by using this code

def store_corr(store_num):
  subset= df[df['store']== store_num]
  subset= subset[['day','dollar_sales']]
  subset_corr = subset.corr()
  corr_val = subset_corr.iloc[0,1]
  return corr_val

And Next step is running for loop each store like this

store = df.store.unique()    
   for i in store:
      corre = store_corr(i)
      print(corre)

But All I need are columns that provide store_num and corr for each store, so How I can coding to get output like this:


store corre
1 0.26
2 0.12
3 -0.96

Thank you

1 Answers1

1

I think you could do this:

store = df.store.unique() 
values = {"store": [], "corre": []}   
for i in store:
    corre = store_corr(i)
    values["store"].append(i)
    values["corre"].append(corre)
    print(i, corre)
df = pd.DataFrame(values)

df dataframe should have the output you want

Lucas M. Uriarte
  • 2,403
  • 5
  • 19