輸入數(shù)據(jù)框: Id Score Score10 19138359 0.5347029367015973 0.8324284744431 12134001 0.9347094453553113 0.632535428479以下數(shù)據(jù)框需要作為輸出: Id Scores0 19138359 {'Score': 0.5347029367015973, 'Score1': 0.832428474443}1 12134001 {'Score': 0.9347094453553113, 'Score1': 0.632535428479} Scores = ['Score', 'Score1'] d, l = {}, [] for i, row in publish.iterrows(): d['Id'] = row['Id'] d['Scores'] = row[Scores].to_dict() l.append(d)它給出了錯(cuò)誤的輸出!
1 回答

森欄
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
使用orient="records"參數(shù)to_dict:
# Get score columns
score_columns = df.filter(like='Score').columns
# Create dict of scores column
df['Scores'] = df[score_columns].to_dict(orient='records')
# Drop original score columns
df.drop(columns=score_columns, inplace=True)
[出去]
Id Scores
0 19138359 {'Score': 0.5347029367015973, 'Score1': 0.8324284744429999}
1 12134001 {'Score': 0.9347094453553112, 'Score1': 0.632535428479}
添加回答
舉報(bào)
0/150
提交
取消