2

Hello World,

I would to create One Dataframe per unique value from one column.

Below is my initial Dataframe.

Date     Name     Value
01/19     Jean        15
01/19     Jean        15
01/17     Bean        15
01/16     Bean        15
01/19     Jean        15

I have found some answers concerning the creation of dataframes based on unique values, such as : Create Pandas DataFrames from Unique Values in one Column.

With the following code, I am having everything within a list.

dfa = [x for _, x in df.groupby('name')]
dfa[0]

output 
Date     Name     Value
01/19     Jean        15
01/19     Jean        15
01/19     Jean        15
Date     Name     Value
01/17     Bean        15
01/16     Bean        15    

However, I would like to assign the name of dataframe automatically and not doing:

df_Jean= df[0]
df_Bean= df[1]

Below are the expected output

df_Jean
Date     Name     Value
01/19     Jean        15
01/19     Jean        15
01/19     Jean        15
A2N15
  • 595
  • 4
  • 20

2 Answers2

2

You should use a dict:

d = {k:v for k, v in df.groupby("Name")}

Then you can simply pass the names as key:

print (d["Jean"])

    Date  Name  Value
0  01/19  Jean     15
1  01/19  Jean     15
4  01/19  Jean     15
Alon
  • 10,381
  • 23
  • 88
  • 152
1

It is possible, but not recommended in python:

for i, g in df.groupby('name'):
    globals()['df_' + str(i)] =  g

Or:

for i, g in df.groupby('name'):
    globals()[f'df_{i}'] =  g

Better is use dict:

d = dict(tuple(df.groupby("Name")))

print (d['Jean'])
print (d['Bean'])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • thanks @jezrael, this do the job :) However, do you know how to name each dataframe as df1, df2, .. instead of names (might be more efficient if I want to loop after) – A2N15 Jan 07 '20 at 08:54