사전에 사전을 사용하고 있습니다. pandas 데이터 프레임을 반복하면서 액션 행의 값은 사전의 키 중 하나와 항상 일치하며 그 행의 다른 값이 해당 사전의 목록에 추가됩니다. 어떤 이유로, 그러나, 다른 값이 사전python이 모든 사전에 추가됩니다.
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)
이 sheetstats 내부의 모든 사전 동일 비율의 모든 것 모두리스트에 추가된다. 왜?
* 동일한 사전 * :'sheetstats [action] = general_form'을 사용하고 있기 때문에. –
'sheetstats [action] = general_form'은 모든 사전 슬롯에 같은 사전을 넣고 싶다고 말하고 있습니다. 당신이'general_form'을 복사하고 싶다고 생각합니다. – MooingRawr