Hello, I'm trying to compare the kappa scores for one person against other people. I want to generate a dataframe that has the kappa for person A vs person B, person A vs person C, person A vs person D, etc.
To do this, I was thinking of reading in a everyone's scores that they gave as a dataframe, and assigning them to a dictionary. So something like {person B : person_B_scores_df, person C: person_C_scores}
But I can't figure out how to iterate over a for loop and add the key pairings to a dictionary. I keep getting a key error. Here is my code.
## Create list of raters
rater_list = ["person_A", "person_B", "person_C", "person_D"]
rater_dict = {}
## Pull in each rater's scores and add them key/value pairing in rater_dict
for name in rater_list:
rater_df = pd.read_csv(input_path/f"rater_scores_{name}.csv")
rater_dict[name].append(rater_df)
## Also tried the following!! - The code below only appends the last person in the rater list to the dictionary
#for name in rater_list:
# rater_df = pd.read_csv(input_path/f"rater_scores_{name}.csv")
# rater_dict={name:rater_df}python 3.x - How to add key and value pairs to new empty dictionary using for loop? - Stack Overflow
python - Adding item to Dictionary within loop - Stack Overflow
Looping through Dictionary and add items to second dictionary
python - Iterating over a dictionary using a 'for' loop, getting keys - Stack Overflow
In Python, dictionary keys are unique. If you assign a value to the same key again, it gets overwritten. Example :
dct = {}
dct["key"] = "value"
print(dct)
dct["key"] = "value2"
print(dct)
Output :
{"key": "value"}
{"key": "value2"}
The only option you have is to create a list of amounts and a list of ids:
new_debt_dic = {"amounts": [], "ids": []}
for i in debts_list:
new_debt_dic["amounts"].append(i["amount"])
new_debt_dic["ids"].append(i["id"])
print(new_debt_dic)
Output :
{"amounts": [123.46, 100, ...], "ids": [0, 1, ...]}
Each value of debts_list is dictionary with only 2 key-value pairs, where the keys are amount or id. I think you meant to use the id as your keys - if so, you should use the following:
new_debt_dic = {}
for d in debts_list:
new_debt_dic[d['id']] = d['amount']
In your current code, what Dictionary.update() does is that it updates (update means the value is overwritten from the value for same key in passed in dictionary) the keys in current dictionary with the values from the dictionary passed in as the parameter to it (adding any new key:value pairs if existing) . A single flat dictionary does not satisfy your requirement , you either need a list of dictionaries or a dictionary with nested dictionaries.
If you want a list of dictionaries (where each element in the list would be a diciotnary of a entry) then you can make case_list as a list and then append case to it (instead of update) .
Example -
case_list = []
for entry in entries_list:
case = {'key1': entry[0], 'key2': entry[1], 'key3':entry[2] }
case_list.append(case)
Or you can also have a dictionary of dictionaries with the key of each element in the dictionary being entry1 or entry2 , etc and the value being the corresponding dictionary for that entry.
case_list = {}
for entry in entries_list:
case = {'key1': value, 'key2': value, 'key3':value }
case_list[entryname] = case #you will need to come up with the logic to get the entryname.
As per my understanding you want data in dictionary as shown below:
key1: value1-1,value1-2,value1-3....value100-1
key2: value2-1,value2-2,value2-3....value100-2
key3: value3-1,value3-2,value3-2....value100-3
for this you can use list for each dictionary keys:
case_list = {}
for entry in entries_list:
if key in case_list:
case_list[key1].append(value)
else:
case_list[key1] = [value]
key is just a variable name.
Copyfor key in d:
will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following:
For Python 3.x:
Copyfor key, value in d.items():
For Python 2.x:
Copyfor key, value in d.iteritems():
To test for yourself, change the word key to poop.
In Python 3.x, iteritems() was replaced with simply items(), which returns a set-like view backed by the dict, like iteritems() but even better.
This is also available in 2.7 as viewitems().
The operation items() will work for both 2 and 3, but in 2 it will return a list of the dictionary's (key, value) pairs, which will not reflect changes to the dict that happen after the items() call. If you want the 2.x behavior in 3.x, you can call list(d.items()).
It's not that key is a special word, but that dictionaries implement the iterator protocol. You could do this in your class, e.g. see this question for how to build class iterators.
In the case of dictionaries, it's implemented at the C level. The details are available in PEP 234. In particular, the section titled "Dictionary Iterators":
Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. [...] This means that we can write
Copyfor k in dict: ...which is equivalent to, but much faster than
Copyfor k in dict.keys(): ...as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.
Add methods to dictionaries that return different kinds of iterators explicitly:
Copyfor key in dict.iterkeys(): ... for value in dict.itervalues(): ... for key, value in dict.iteritems(): ...This means that
for x in dictis shorthand forfor x in dict.iterkeys().
In Python 3, dict.iterkeys(), dict.itervalues() and dict.iteritems() are no longer supported. Use dict.keys(), dict.values() and dict.items() instead.
Hi everyone,
Working on a large program right now, and it'll be a little while before I can " test" what I'm doing .
Can I loop through a dictionary and add it's values to a set?