Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

python - How to make a dictionary from the pandas data frame?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a new column first using apply function, then use that column to group and format data

df['gkey'] = df.apply(lambda row: row['col1'].split(',')[0], axis=1)
l = []
for key, value in df.groupby('gkey'):
    l.append({'key': key, 'Results': [{row['col1'].split(',')[1]: row['fre']}for index, row in value.iterrows()]})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...