Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name pandas dataframes in a loop by their key in python?

I would like to create data-frames in a loop, but name each data-frame using a key as not to overwrite each datafranme in the loop.

Here is a simplified version of my data-frame:

ID  Field  Value
1     A     1.1
2     A     1.2
3     A     2.4
4     B     1.7
5     B     4.3
6     C     2.2

So in this case I would like to end up with 3 data frames named A, B and C so this is what I tired:

df2= df.groupby(['Field'])
for key, group in df2:
   key = group.reset_index()

But ofcourse the name 'key' gets overwritten with each sucessive loop. How can I name each dataframe in the loop by its key?

I would lalso like to create a list of the created dataframes as to keep track of them.

like image 668
Gman Avatar asked Dec 25 '22 16:12

Gman


2 Answers

You want to store your objects in a dict:

df_dict = {}
for key, group in df2:
   df_dict[key] = group.reset_index()
like image 182
maxymoo Avatar answered Jan 21 '23 09:01

maxymoo


Using dictionary comprehension, a more succinct solution is as follows:

df_new = {field: df.loc[df.Field == field, :] for field in df.Field.unique()}
like image 21
Alexander Avatar answered Jan 21 '23 08:01

Alexander