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.
You want to store your objects in a dict:
df_dict = {}
for key, group in df2:
df_dict[key] = group.reset_index()
Using dictionary comprehension, a more succinct solution is as follows:
df_new = {field: df.loc[df.Field == field, :] for field in df.Field.unique()}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With