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
我認為這些足夠的細節,請幫我寫這個功能,我是新手處理這個
uj5u.com熱心網友回復:
使用附加資訊:嘗試
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]
我猜你會更換results與sensi.results增加的結果后sensi。
評論中的其他問題:
cols = ["conversions", "weekly_active_customers", "box_count"]
df[[f"{c} 2023 vs 2022" for c in cols]] = df.groupby("market")[cols].pct_change()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360309.html
