Reputation: 29
I have a dictionary with 44 participants, and then 16 metrics for each participant. At the moment there are no values in the dictionary and it looks like this for one participant's id-
{1010437.0: {'acc_nl_f': (), 'acc_nl_m': (), 'acc_ew_m': (), 'acc_ew_f': (), 'acc_eh_m': (), 'acc_eh_f': (), 'acc_cp_m': (), 'acc_cp_f': (), 'acc_ph_m': (), 'acc_ph_f': (), 'rt_nl_f': (), 'rt_nl_m': (), 'rt_ew_m': (), 'rt_ew_f': (), 'rt_eh_m': (), 'rt_eh_f': (), 'rt_cp_m': (), 'rt_cp_f': (), 'rt_ph_m': (), 'rt_ph_f': ()}
I have a data frame with the participant's ID, and 'acc_nl_m' values (as shown in the image below). I want to put these values in the dictionary.
So far, I have tried the following:
for participant in participantDictionary:
for row in range(len(df)):
if df.participant_private_id[row] == participant:
participantDictionary[participant]['acc_nl_m'] = df.response[row]
my error is simply "KeyError: 1", and when I print the dictionary, I do have values for the key 'acc_nl_m', but they are all zero. I think I have not properly linked this to the value I intend?
Any help would be brilliant! Thanks in advance.
Upvotes: 0
Views: 58
Reputation: 1747
Is this a pandas
dataframe by any chance?
If so, and all your columns are also things you want in the dictionary, and assuming participant_private_id
is the index on your dataframe then this is actually far simpler than your code. It can be done in one line:
participantDictionary = df.to_dict(orient='index')
I notice that your dictionary keys are actually subtly different to the column names based on your screenshot. The code I've given will make the keys the column names, so if you wanted the keys you've got in your example dict then you just need to rename the columns before the to_dict
call using:
df.rename(columns={"accuracy_nl_m": "acc_nl_m", ...}, inplace=True)
Upvotes: 1