我正在嘗試創建來自 45 家不同商店的銷售資料的資料框。我將資料保存為串列中的資料幀:所以我有一個長度為 45 的串列,每個元素都是一個 143 個數字的資料幀。
我想要什么:為每個商店創建一個帶有索引列(日期)和單獨列的資料框。
實作目標的最簡單方法是什么?
store1 = df_sales_shops[df_sales_shops['Store'] == 1]
index = pd.DataFrame(store1.index)
index = index.drop_duplicates()
stores_revenue = []
for store in range(45):
temp = df_sales_shops[df_sales_shops['Store'] == store 1]
stores_revenue.append(pd.DataFrame(temp.groupby(['Date']).Weekly_Sales.sum()))
stores_revenue[store].reset_index(drop=True, inplace=True)
print(stores_revenue)
print(index)
輸出商店收入[0]:
[ Weekly_Sales
0 1643690.90
1 1641957.44
2 1611968.17
3 1409727.59
4 1554806.68
.. ...
138 1437059.26
139 1670785.97
140 1573072.81
141 1508068.77
142 1493659.74
[143 rows x 1 columns],
...
uj5u.com熱心網友回復:
IIUC,下面的代碼應該相當于你的回圈和你期望的:
out = df[df['Store'].between(1, 45)].groupby(['Store', 'Date'])['Weekly_Sales'].sum() \
.unstack(level='Store').reset_index(drop=True) \
.rename_axis(columns=None).add_prefix('Store')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/369016.html
下一篇:python用條件求和一列的值
