I am trying to do descriptive statistics of a DataFrame using GroupBy, and put those values back into the DataFrame.
My DataFrame contains a non-unique running number which identifies a person (anonymously), and some values connected to each person.
Eg:
RunNr Value
1 126
1 158
1 18
2 65
3 31
3 4
By using GroupBy I can calculate descriptive statistics for each person(running number), like standard deviation. I want to add these back into the DataFrame for further processing (like making a report in Word).
The result should look like this:
RunNr Value Std
1 126 59,9
1 158 59,9
1 18 59,9
2 65 Nan
3 31 13,5
3 4 13,5
The best solution I have come up with is to calculate standard deviation (and other statistics), put these into a dictionary where the running number is the key and the value is the standard deviation.
I now have a dictionary where the running number in the dictionary is a unique key, while it is not in the DataFrame. My next step is to iterate over the dictionary, and use .loc() to insert the corresponding value into the correct row:
for key, value in self.dict_of_std:
self.internal_main_df.loc[self.internal_main_df.Fnr == key] = value
I am getting this error:
TypeError: cannot unpack non-iterable float object
Suggestions to improve my code, or my overall method is appreciated.