簡體   English   中英

將熊貓 groupby 對象保存到 csv 文件中

[英]save a pandas groupby object into a csv file

我有一個我無法弄清楚的問題,盡管我閱讀了類似的帖子,例如Pandas groupby 到 to_csv 它對我不起作用。 我正在嘗試編寫代碼將每個組與 groupby 對象分開,並將每個組保存到自己的 Excel 電子表格中。

我附上了一個代碼的玩具示例,我這樣做是為了在帶有一些列的熊貓上獲取我的 groupby 對象。

現在,我需要將此對象中的每個組保存到單獨的 csv 文件中,或者至少保存在 excel 中的單獨工作表中。

dff = pd.DataFrame({'SKU': ['001', '002', '003'],
                    'revenue_contribution_in_percentage': [0.2, 0.5, 0.3],
                    'BuyPrice' : [2,3,4],
                    'SellPrice' : [5,6,6],
                    'margin' : [3,3,2],
                    'Avg_per_week' : [3,2,5],
                    'StockOnHand' : [4,10,20],
                            'StockOnOrder': [0,0,0],
                            'Supplier' : ['ABC', 'ABC', 'ABZ' ],
                            'SupplierLeadTime': [5,5,5],
                            'cumul_value':[0.4,0.6,1],
                            'class_mention':['A','A','B'],
                            'std_week':[1,2,1],
                            'review_time' : [2,2,2],
                            'holding_cost': [0.35, 0.35, 0.35],
                            'aggregate_order_placement_cost': [200, 230,210]
})

我已完成以下操作以獲取 groupby 供應商對象

groups = [group.reset_index().set_index(['SKU'])[[
                            'revenue_contribution_in_percentage',
                            'BuyPrice',
                            'SellPrice',
                            'margin',
                            'Avg_per_week',
                            'StockOnHand',
                            'StockOnOrder',
                            'Supplier',
                            'SupplierLeadTime',
                            'cumul_value',
                            'class_mention',
                            'std_week',
                            'review_time',
                            'holding_cost',
                            'aggregate_order_placement_cost',
                            'periods']] for _, group in dff.groupby('Supplier')]

df_group = pd.DataFrame(groups).sum()
group_to_excel = df_group.to_csv('results.csv')

我想得到的輸出是以下內容:兩個不同的數據集,可以以 csv 格式保存,如下所示:

   SKU  revenue_contribution_in_percentage  BuyPrice  SellPrice  margin  \
0  001                                 0.2         2          5       3   
1  002                                 0.5         3          6       3   

   Avg_per_week  StockOnHand  StockOnOrder Supplier  SupplierLeadTime  \
0             3            4             0      ABC                 5   
1             2           10             0      ABC                 5   

   cumul_value class_mention  std_week  review_time  holding_cost  \
0          0.4             A         1            2          0.35   
1          0.6             A         2            2          0.35   

   aggregate_order_placement_cost  
0                             200  
1                             230  

   SKU  revenue_contribution_in_percentage  BuyPrice  SellPrice  margin  \
0  003                                 0.3         4          6       2   

   Avg_per_week  StockOnHand  StockOnOrder Supplier  SupplierLeadTime  \
0             5           20             0      ABZ                 5   

   cumul_value class_mention  std_week  review_time  holding_cost  \
0            1             B         1            2          0.35   

   aggregate_order_placement_cost  
0                             210  

在這一點上,我的代碼給出了一個也是唯一的工作表(可怕的工作表),上面幾乎沒有任何內容。 我不確定此時有什么問題。 我將不勝感激這方面的一些幫助! 多謝!

您不需要groupby因為您沒有聚合任何東西。 您真正想要的是按每個唯一的供應商切片dff並將它們導出到他們自己的文件中。 嘗試這個:

cols = [
    'SKU',
    'revenue_contribution_in_percentage',
    'BuyPrice',
    'SellPrice',
    'margin',
    'Avg_per_week',
    'StockOnHand',
    'StockOnOrder',
    'Supplier',
    'SupplierLeadTime',
    'cumul_value',
    'class_mention',
    'std_week',
    'review_time',
    'holding_cost',
    'aggregate_order_placement_cost'
]

for supplier in dff['Supplier'].unique():
    sub_dff = dff[dff['Supplier'] == supplier][cols]
    sub_dff.to_csv(f'{supplier}_data.csv')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM