0

I have a dictionary which looks like this:

dic = {'shop_2':           date  shop_id  item_id    item_price
                 0   2013-05-12     2    20949         5.0             
                 1   2013-05-13     2    20949         5.0             
                 2   2013-05-17     2    20949         5.0             
                 3   2013-05-19     2    20949         5.0             
                 4   2013-05-21     2    20949         5.0             

 [568 rows x 7 columns],

 'shop_3':           date  shop_id  item_id  item_price
           0   2013-04-24     59    20949         5.0              
           1   2013-04-26     59    20949         5.0             
           2   2013-04-27     59    20949         5.0             
           3   2013-04-28     59    20949         5.0             
           4   2013-04-29     59    20949         5.0             

 [760 rows x 7 columns]}

What I'm trying to do is to create a loop yn Python to split the dictionary by its keys into new dataframes. For example, the dataframe shop_2 must have the values of key: shop_2 from the dictionary, the dataframe shop_3 must have the values of key: shop_3 from the dictionary. If I try to access to the keys of the dictionary it already looks like a df but what I need is a loop to create new dataframe for each one of the keys from the dictionary.

brenda
  • 656
  • 8
  • 24
  • kindly trim the data u shared to a few lines. makes it easy to get a possible solution – sammywemmy Apr 08 '20 at 01:53
  • couldnt copy the data. give this a try : ```[value.assign(key = key) for key,value in dic.items()]``` – sammywemmy Apr 08 '20 at 04:32
  • Can you explain how does it works? the output is a list of the values of the dictionary. This is, instead of accessing to the data inside it by using the keys, now I just have to use the indices and I can manipulate it in an easier way, however the goal is still not reached. – brenda Apr 08 '20 at 14:49
  • the value is the dataframe itself, which u can get by just running ```[value for key,value in dic.items()]``` the assign simply hooks the key back to the dataframe. if the goal is not reached, post a visual of what u want to achieve. also use [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well, cos ur shared data cant be replicated – sammywemmy Apr 08 '20 at 20:32

1 Answers1

0

It looks like this is a dictionary where the key is a string and the value is a DataFrame. It is not clear what you want to do, so I'll offer some options.

If you want to iterate over the dataframes, you would use:

for df in dic.values():
    # do something with the dataframe

If you just need a list of dataframes, you would use:

dfs = list(dic.values())

Or if you want to access an individual dataframe

shop_2_df = dic.get('shop_2')
Eric Truett
  • 2,970
  • 1
  • 16
  • 21
  • What I need to do is the last line you wrote, however, my dictionary has at least 60 keys, that's why I wanted to do a loop which generates each df per key from the dictionary – brenda Apr 08 '20 at 02:35