I am using dictoinaries inside of dictionaries. Looping through a pandas dataframe, the value of the action row always matches one of the keys in the dictionary, and based on that some other values from that row are appended to a list in that dictionary. For some reason, however, the values are appended to all lists in the other dictionaries
general_form = {
"Percentage": np.nan, "DayPercentage": np.nan, "sample_size": np.nan, "Percentages": [], "DayPercentages": []
}
#get all possible action types
action_types = self.df['Action Type'].tolist()
action_types = list(set(action_types))
#give every action type its own dictionary within the main dictionary
sheetstats = {}
for action in action_types:
sheetstats[action] = general_form
#push the percentage in the list inside the dictionary specified in
#action
for index, row in self.df.iterrows():
percentage = row['Percentage']
daypercentage = row['DayPercentage']
action = row['Action Type']
sheetstats[action]['Percentages'].append(percentage)
sheetstats[action]["DayPercentages"].append(daypercentage)
This will make all of the percentages identical in all dictionaries inside sheetstats. Why?
sheetstats[action] = general_form
.sheetstats[action] = general_form
is saying you want to put the same dictionary in every key slot... I think you wanted to copygeneral_form