2017-03-08 83 views
1

難以忍受。設想一個數據框,其中包含多個產品類別「標籤」,其中一些列表位於列表中,如下所示。PANDAS使用多個組在單列內使用多個組

import pandas as pd 

raw = { 
    'Products' : ['Rock On Leather Journal', 
        'Beats Earbuds In Ear Timer', 
        'Garmin 25mm Wristwatch' 
       ], 
    'Product Cost': [55,163,200], 
    'Product Category' : [['Music','Journals','Paper'], 
          ['Headphones','Music', 'Clocks'], 
          ['Watches','Clocks']] 
} 

data = pd.DataFrame(raw) 

什麼是最好的方法來計算每個類別中有多少產品,並平均每個類別的成本?例如,

音樂:數量?,平均價格?

通常,這將是一個使用地圖或groupby的簡單過程。但類別列中列表的出現增加了一個棘手的轉折。

回答

2

來源DF:

In [21]: data 
Out[21]: 
       Product Category Product Cost     Products 
0  [Music, Journals, Paper]   55  Rock On Leather Journal 
1 [Headphones, Music, Clocks]   163 Beats Earbuds In Ear Timer 
2   [Watches, Clocks]   200  Garmin 25mm Wristwatch 

首先讓變換(平整)到以下DF:

In [22]: lst_col = 'Product Category' 
    ...: 
    ...: x = pd.DataFrame({ 
    ...:  col:np.repeat(data[col].values, data[lst_col].str.len()) 
    ...:  for col in data.columns.difference([lst_col]) 
    ...: }).assign(**{lst_col:np.concatenate(data[lst_col].values)})[data.columns.tolist()] 
    ...: 

In [23]: x 
Out[23]: 
    Product Category Product Cost     Products 
0   Music   55  Rock On Leather Journal 
1   Journals   55  Rock On Leather Journal 
2   Paper   55  Rock On Leather Journal 
3  Headphones   163 Beats Earbuds In Ear Timer 
4   Music   163 Beats Earbuds In Ear Timer 
5   Clocks   163 Beats Earbuds In Ear Timer 
6   Watches   200  Garmin 25mm Wristwatch 
7   Clocks   200  Garmin 25mm Wristwatch 

現在我們可以很容易地"count of how many Products within each Category, and to average the costs for each category"

In [25]: x.groupby('Product Category')['Product Cost'].agg(['size', 'mean']).reset_index() 
Out[25]: 
    Product Category size mean 
0   Clocks  2 181.5 
1  Headphones  1 163.0 
2   Journals  1 55.0 
3   Music  2 109.0 
4   Paper  1 55.0 
5   Watches  1 200.0 

幾點說明:

利用這些信息,我們可以複製所有非列表列如下

In [7]: data[lst_col].str.len() 
Out[7]: 
0 3 
1 3 
2 2 
Name: Product Category, dtype: int64 

:每排

數列表中的元素現在

In [3]: x = pd.DataFrame({ 
    ...:  col:np.repeat(data[col].values, data[lst_col].str.len()) 
    ...:  for col in data.columns.difference([lst_col]) 
    ...: }) 

In [4]: x 
Out[4]: 
    Product Cost     Products 
0   55  Rock On Leather Journal 
1   55  Rock On Leather Journal 
2   55  Rock On Leather Journal 
3   163 Beats Earbuds In Ear Timer 
4   163 Beats Earbuds In Ear Timer 
5   163 Beats Earbuds In Ear Timer 
6   200  Garmin 25mm Wristwatch 
7   200  Garmin 25mm Wristwatch 

我們可以添加扁平list column

In [8]: np.concatenate(data[lst_col].values) 
Out[8]: 
array(['Music', 'Journals', 'Paper', 'Headphones', 'Music', 'Clocks', 'Watches', 'Clocks'], 
     dtype='<U10') 

In [5]: x.assign(**{lst_col:np.concatenate(data[lst_col].values)}) 
Out[5]: 
    Product Cost     Products Product Category 
0   55  Rock On Leather Journal   Music 
1   55  Rock On Leather Journal   Journals 
2   55  Rock On Leather Journal   Paper 
3   163 Beats Earbuds In Ear Timer  Headphones 
4   163 Beats Earbuds In Ear Timer   Music 
5   163 Beats Earbuds In Ear Timer   Clocks 
6   200  Garmin 25mm Wristwatch   Watches 
7   200  Garmin 25mm Wristwatch   Clocks 

最後我們簡單地選擇原始順序中的列:

In [6]: x.assign(**{lst_col:np.concatenate(data[lst_col].values)})[data.columns.tolist()] 
Out[6]: 
    Product Category Product Cost     Products 
0   Music   55  Rock On Leather Journal 
1   Journals   55  Rock On Leather Journal 
2   Paper   55  Rock On Leather Journal 
3  Headphones   163 Beats Earbuds In Ear Timer 
4   Music   163 Beats Earbuds In Ear Timer 
5   Clocks   163 Beats Earbuds In Ear Timer 
6   Watches   200  Garmin 25mm Wristwatch 
7   Clocks   200  Garmin 25mm Wristwatch 
+0

這個工程!儘管如此,我仍然試圖通過.assign()完全理解「for」col部分發生的情況。它看起來像是每個類別發生的事情,您正在將行數據複製到一個新行中,以便每行都有一個類別。然後,使用.assign()添加所有其他列。但也許我錯了。這比我見過的任何事情都要複雜得多(儘管很棒),我希望你會爲看到這篇文章的其他人解釋一下。 – Adestin

+0

@Adestin,我已經添加了一些解釋 - 請檢查 – MaxU

0

這取決於你的情況。如果它是這個大小,你可能想爲每個元素創建一個布爾列 - 例如

unique_products = set(chain(*data['Product Category'])) 
for product in unique_products: 
    data['product_{}.format(product) = data.Products.apply(lambda x: product in x) 

或者,如果您有大量產品,請根據需要繼續使用data.Product.apply(lamba x: product in x)。您也可以使用data.Product.isin([product_one, product_two])執行類似的檢查。

一旦你有一個合成列,你可以使用它合併。