I am looking to chunk a bunch of data from a dataframe. In order to do so, I need to define a dynamic name to a dictionary.
I would like to do something like:
dict_{}.format(VARIABLE_NAME) = {}
The above shown is an illegal operation. How can I go about defining a new dictionary name every time I need to create one? This is happening in a for loop, so I need to use dynamic dict names. Let me know if there is anything else I need to provide.
Here is a snippet of the dataframe
REFERENCE_CODE TRANSLATION
0 ladder_now NaN
1 0 xyzwu
2 1 yxzuv
3 2 asdfasd
4 3 sdfsdh
5 4 hghffg
6 5 agfdhsj
7 6 dfgasgf
8 7 jfhkgj
9 8 djfgjfhk
10 9 dsfasys
11 10 kghkfdy
12 98 dsfhsuert
13 99 wsdfadjs
14 country_satis Sa pangkagab’san, aoogma po ba kamo o dai naoo...
15 1 Naoogma
16 2 Dai naoogma
17 8 Dai aram (HUWAG BASAHIN)
18 9 Huminabo (HUWAG BASAHIN)
19 NaN NaN
I am trying to take chunks of data, as in, take ladder_now
and all the values associated with it, then find country_satis
and take those values, put them in a separate dictionary. Here is the logic I have.. just missing the dynamically created dict:
for index, row in df.iterrows():
j = 0
if isinstance(row['REFERENCE_CODE'], str):
if j == 0:
# fix dynamically changing dict here
trend_dict = {}
trend_dict[row['REFERENCE_CODE']] = row['TRANSLATION']
else:
j = 0
# create new dynamically named dictionary
next_dict = {}
next_dict[row['REFERENCE_CODE']] = row['TRANSLATION']
else:
trend_dict[row['REFERENCE_CODE']] = row['TRANSLATION']
j += 1
So essentially, I would like to have dict_ladder_now
as one dictionary which contains all key, value pairs of everything below it until it reaches country_satis
, and then a dict_country_satis
as another.