输入数据框: Id Score Score10 19138359 0.5347029367015973 0.8324284744431 12134001 0.9347094453553113 0.632535428479以下数据框需要作为输出: 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)它给出了错误的输出!
1 回答
森栏
TA贡献1810条经验 获得超5个赞
使用orient="records"参数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}
添加回答
举报
0/150
提交
取消