0

I have a pandas data frame I need to chunk up into ten smaller sub-sets and I am doing it as follows:

df_1, df_2, df_3, df_4, df_5, df_6, df_7, df_8, df_9, df_10 = np.array_split(processed_data, 10)

This is horrible. Is there a way of generating the variable names via an expression?

For example:

df[some expression to evaluate n] = np.array_split(processed_data, n)

I've tried various things and have not gotten anywhere. Any insight would be appreciated. Thanks!

Sandy Lee
  • 221
  • 1
  • 9
  • 2
    You can create a dictionary with `enumerate` for such usecase: `df_arr = np.split(df,10)` then `df_dict = {f"df{e}": df for e,df in enumerate(df_arr,1)}` , then refer each df by `df_dict ['df1']` check [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) , you could also create locals/globals `for e,df in enumerate(df_arr,1):locals().update({f"df{e}":df}) ` but using dictionary is a better choice IMO. – anky Dec 31 '20 at 10:25
  • Also a good read [Why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – anky Dec 31 '20 at 10:33

0 Answers0