0

I created this code below to extract each individual key as separate data frames. However, since the prim_keys contains colon in their name, e.g. '5091016:2', the resulting dataframes should be named something like data5091016:2. But, it is impossible to access these datafarmes just typing that. How can I access these dataframes?

primkeys = list(data.prim_key.unique())
grouped = data.groupby(['prim_key'])

for i in primkeys:
    globals()[f'data{i}'] = grouped.get_group(i)

Thanks already. Best.

  • 3
    Try to create a dict or replace `:` by `_` – Corralien Mar 26 '23 at 18:35
  • 2
    Don’t create global variables, but keys in a dict…!? Which you’re kinda doing anyway… – deceze Mar 26 '23 at 18:37
  • 1
    You could do `globals()["data5091016:2"]`, especially if you want an animated conversation at your next code review. First, why put dynamic data in globals? You don't know the names in advance so its not like you'll be typing them directly into your program. There are reasons for doing this, perhaps you are using a shell of some sorts, so its not like its never a good idea. – tdelaney Mar 26 '23 at 18:46
  • I guess I must learn data structures as soon as possible. Thanks everyone for your valuable comments. – alperyildirim Mar 26 '23 at 18:59

2 Answers2

2

You can use a dict instead of create dynamically dataframes:

dfs = dict(list(df.groupby('prim_key')))

Now you can use dfs['5091016:2'].

Corralien
  • 109,409
  • 8
  • 28
  • 52
1

You can set whatever key you want in globals(), but if it’s not an identifier (or isn’t even a string!) it isn’t a variable because the language can’t access it (short of manually constructing bytecode objects). If you want to change your keys to be identifiers (beyond prefixing with a letter, as you have already done), go for it.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76