-1

here is what I am trying to do. I have one dataframe called df_prod_pivot_cluster, with one column named 'cluster', with values ranging from 1 to 25. I want to create 25 dataframes. I wrote the piece of code below, but how to do that in a more elegant way (for example: For x in range(1, 25):)?

ts_cluster1 = df_prod_pivot_cluster.loc[df_prod_pivot_cluster['cluster'] == 1]
ts_cluster1 = ts_cluster1.drop(columns='EASTING','NORTHING'])
ts_cluster1 = ts_cluster1.fillna(0)
ts_cluster1 = ts_cluster1.sum()

ts_cluster2 = df_prod_pivot_cluster.loc[df_prod_pivot_cluster['cluster'] == 2]
ts_cluster2 = ts_cluster2.drop(columns=['EASTING', 'NORTHING'])
ts_cluster2 = ts_cluster2.fillna(0)
ts_cluster2 = ts_cluster2.sum()
.
.
.

ts_cluster25 = df_prod_pivot_cluster.loc[df_prod_pivot_cluster['cluster'] == 25]
ts_cluster25 = ts_cluster25.drop(columns=['EASTING', 'NORTHING'])
ts_cluster25 = ts_cluster25.fillna(0)
ts_cluster25 = ts_cluster25.sum()
Pierre
  • 13
  • 4
  • 1
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Pranav Hosangadi Sep 12 '20 at 19:10
  • This is a very basic question. Initialise an empty list. Make a `for` loop. Inside the `for` loop, create a dataframe and then append it to the list. Do not create 25 separate variables, it is pointless. – alani Sep 12 '20 at 19:56
  • thank alani,but know do i name the new dataframe? I want the dataframe to be called: 'ts_cluster1', 'ts_cluster2', etc.. – Pierre Sep 12 '20 at 20:03

1 Answers1

0

this code works for me:

d = {}
for i in range(1, 26):
    d["ts_cluster{}".format(i)] = df_prod_pivot_cluster.loc[df_prod_pivot_cluster['cluster'] == i]
    d["ts_cluster{}".format(i)] = pd.DataFrame(d["ts_cluster{}".format(i)].sum())

Pierre
  • 13
  • 4