0

I have a dictionary that contains 6 dataframes stored as values. I'd like to convert these values into actual dataframes with the name of the dataframe being the key corresponding to the values. The dictionary structure is like this:

Name: frames, Type: dict, Size: 6
'_20Nov2017_24Dec2017':[1220608 rows x 4 columns]
'_25Dec2017_28Jan2018':[1931572 rows x 4 columns]
...etc

The desired output is 6 dataframes, with the names _20Nov2017_24Dec2017, 25Dec2017_28Jan2018 and so on. Here is a snapshot of the dataframes if needed:

UpdateDatetime   Code   SaleTime      Status
05JAN18:12:37    BSNAS  03JAN18:19:55 N

Any ideas how to go about this? Thanks!

natnay
  • 460
  • 1
  • 5
  • 24
  • 3
    It is recommended by the community at large that this is a bad idea. It requires mucking with `globals()`. But we've all seen this question time and again and it always comes down to, "Use a dictionary. But if you insist:..." – piRSquared Feb 19 '18 at 20:16
  • "I'd like to convert these values into actual dataframes" when you say "actual dataframes", what, **exactly** do you mean by that? I thought they *were* data-frames... no? In which case, **why not** use the dictionary? – juanpa.arrivillaga Feb 19 '18 at 20:19
  • This answer (and comments) may suit your needs: https://stackoverflow.com/a/11553769/9209546 – jpp Feb 19 '18 at 20:19
  • @jp_data_analysis I added it to the dup list – piRSquared Feb 19 '18 at 20:20
  • @piRSquared, this question is so frequent i think someone should do a more detailed write-up (maybe me!). One or two sentences don't seem to suffice and we keep getting asked for this! – jpp Feb 19 '18 at 20:24
  • @jp_data_analysis go for it. If you do, I'd attach it to the first dup target here. If I like your answer, I'll put a bounty on the question to get it some air time. – piRSquared Feb 19 '18 at 20:25
  • @jp_data_analysis I would appreciate that because I'm not able to follow the "solution" via the link you shared nor why this is a bad idea. – natnay Feb 19 '18 at 20:29
  • @user3910919 the solution is quite simple, *use a container*, i.e. like the `dict` you already have. Why would you want a bunch of separate, dynamically generated variables? How would you even know how to refer to them in your code? – juanpa.arrivillaga Feb 19 '18 at 20:32
  • @juanpa.arrivillaga I don't think you read the question. They wouldn't be dynamically generated. I said "the name of the dataframe being the key corresponding to the values". I showed what two of the keys names were and said I was trying to get datasets having that same name. – natnay Feb 19 '18 at 20:35
  • ... they would be *dynamically generated based on the keys*. And if you already know the keys ahead of time, what's stopping you from doing `same_name_as_key_string = df_dict['same_name_as_key_string']`? But again, *why would you want this*? Why not *use the dictionary*??? – juanpa.arrivillaga Feb 19 '18 at 20:37
  • @juanpa.arrivillaga I guess I don't understand why you would want to use a dictionary in this case? Is there an advantage to having them in a dictionary vs not? It seems to me I can save typing and confusion by not having to say `df_dict['df1']` or `df_dict['df2']`. My plan later is to merge each dataframe in the dictionary onto another set, seems like it would be less messy if they weren't in the dictionary. – natnay Feb 19 '18 at 20:47
  • So, presumably there is *some* reason you are using a dict to begin with. How did they end up in a dictionary in the first place? If you have a bunch of enumerated keys,(only 6 in this instance), why not make them variables from the beginning? Or even after the fact, if you *do* know the names ahead of time, what is stopping you from doint `name1 = df_dict['name1']`? Anyway, if you plan on merging these dictionaries into some *other* container of dictionaries, then it seems *much less messy* to keep them in a container... – juanpa.arrivillaga Feb 19 '18 at 20:49
  • @juanpa.arrivillaga Originally I was trying to import all csv files in a folder as dataframes. Each dataframe would have the name of the csv file (minus the extension). I couldn't figure out how to have them all as "independent" dataframes without putting them in a dictionary. So that's how they ended up in a dictionary. I could definitely do `name1 = df_dict['name1']` 6 different times, but I thought there might be a way to loop over all of them and do it at once without having to hard-code it. So that's why I asked the question. – natnay Feb 19 '18 at 21:01

0 Answers0