[英]Pandas, create dictionary from df, with one column as replacing another
I have an unknown number of DataFrames.我有未知数量的 DataFrame。 two for example:
两个例如:
date week_score daily_score site_name
0 2014-07-04 100 90 demo 2
1 2014-07-05 80 55 demo 2
2 2015-07-06 70 60 demo 2
date week_score daily_score site_name
0 2014-07-04 85 100 demo 1
1 2014-07-05 50 80 demo 1
2 2015-07-06 45 30 demo 1
I know the data frames all have the same shape and columns names.我知道数据框都具有相同的形状和列名。
I want to combine them into a list of dictionaries ( df.to_dict(orient='records'
) but have the site_name as key and to do this for every score.我想将它们组合成一个字典列表(
df.to_dict(orient='records'
),但将 site_name 作为键并为每个分数执行此操作。
the desired output is a bit tricky:所需的 output 有点棘手:
{'week_score: [{'date': '2014-07-04', 'demo 2': 100, 'demo 1': 85},
{'date': '2014-07-05', 'demo 2': 80, 'demo 1': 50},
{'date': '2014-07-06', 'demo 2': 70, 'demo 1': 45}],
'daily_score: [{'date': '2014-07-04', 'demo 2': 90, 'demo 1': 100},
{'date': '2014-07-05', 'demo 2': 55, 'demo 1': 80},
{'date': '2014-07-06', 'demo 2': 60, 'demo 1': 30}],
}
you can try this code:你可以试试这段代码:
d = dict()
for col in df.columns[1:-1].tolist():
new_df = pd.DataFrame({'date':dfs[0]['date']})
for df in dfs:
site_name = df['site_name'].unique()[0]
dropped = df.drop('site_name',axis='columns')
new_df[site_name] = df[col]
d[col] = new_df.to_dict('records')
>>>d
output: output:
{'week_score': [{'date': '2014-07-04', 'demo1': 85, 'demo2': 100},
{'date': '2014-07-05', 'demo1': 50, 'demo2': 80},
{'date': '2015-07-06', 'demo1': 45, 'demo2': 70}],
'daily_score': [{'date': '2014-07-04', 'demo1': 100, 'demo2': 90},
{'date': '2014-07-05', 'demo1': 80, 'demo2': 55},
{'date': '2015-07-06', 'demo1': 30, 'demo2': 60}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.