Python dict包含数据帧,如何将它们分解为单独的行


R1 = {
'mar': 'BS',
'summary': pd.DataFrame({
"year": [2002, 2003],
"customers": [400, 230],
"count": [180, 115]}),
'params': {
'r': 1,
'o': 4
}
}
R2 = {
'mar': 'NY',
'summary': pd.DataFrame({
"year": [2002, 2003],
"customers": [410, 220],
"count": np.array([180, 115])}),
'params': {
'r': 2,
'o': 5
}

将其转换为以下格式的


mar      r   o   year    customers   count
0  NY       0.00    0.0     2002    58      400   
1  NY       0.00    0.0     2003    220         230             
2  BS       0.02    0.0     2002    180         410             
3  BS       0.02    0.0     2003    115         220             

我认为这些足够的细节,请帮助我写这个函数,我是处理这个的新手

附加信息:尝试

results = [RESULT_1, RESULT_2]
df = pd.concat(
(
pd.DataFrame(
{
'market': result['market'],
**result['model_params'],
**result['summary_yearly'].to_dict(orient='list')
}
)
for result in results
),
ignore_index=True
)

或者更好的

results = [RESULT_1, RESULT_2]
f = pd.concat(
(
result['summary_yearly'].assign(
**{'market': result['market'], **result['model_params']}
)
for result in results
),
ignore_index=True
)
df = df[df.columns.to_list()[-3:] + df.columns.to_list()[:-3]]

结果:

market  rete_increase  ...  weekly_active_customers  box_count
0    Boston            0.0  ...                      400      180.0
1    Boston            0.0  ...                      230     1150.0
2  New York            0.0  ...                      410      183.6
3  New York            0.0  ...                      220      117.3
[4 rows x 7 columns]

我想在将结果添加到sensi之后,您应该将results替换为sensi.results


评论中的其他问题:

cols = ["conversions", "weekly_active_customers", "box_count"]
df[[f"{c} 2023 vs 2022" for c in cols]] = df.groupby("market")[cols].pct_change()

相关内容

最新更新