2016-11-14 331 views
0

我有這個測試表中數據幀的大熊貓功能從列選擇大熊貓DF

Leaf_category_id session_id product_id 
0    111   1   987 
3    111   4   987 
4    111   1   741 
1    222   2   654 
2    333   3   321 

enter image description here

我想是

for leaf_category_id 111: 

結果應該是。

session_id product_id 
1   987,741 
4   987 

同樣我可以定義確實爲所有leaf_category ID的相同的功能,我的表包含多個行,它只是它的快照。

回答

1

您可以使用boolean indexing第一,然後用groupby適用join

df = pd.DataFrame({'Leaf_category_id':[111,111,111,222,333], 
        'session_id':[1,4,1,2,3], 
        'product_id':[987,987,741,654,321]}, 
        columns =['Leaf_category_id','session_id','product_id']) 

print (df)     
    Leaf_category_id session_id product_id 
0    111   1   987 
1    111   4   987 
2    111   1   741 
3    222   2   654 
4    333   3   321 


print (df[df.Leaf_category_id == 111] 
      .groupby('session_id')['product_id'] 
      .apply(lambda x: ','.join(x.astype(str)))) 
session_id 
1 987,741 
4  987 
Name: product_id, dtype: object 

編輯的評論:

print (df.groupby(['Leaf_category_id','session_id'])['product_id'] 
     .apply(lambda x: ','.join(x.astype(str))) 
     .reset_index()) 
    Leaf_category_id session_id product_id 
0    111   1 987,741 
1    111   4  987 
2    222   2  654 
3    333   3  321 

或者,如果需要對每個獨特的價值在Leaf_category_idDataFrame

for i in df.Leaf_category_id.unique(): 
    print (df[df.Leaf_category_id == i] \ 
       .groupby('session_id')['product_id'] \ 
       .apply(lambda x: ','.join(x.astype(str))) \ 
       .reset_index()) 

    session_id product_id 
0   1 987,741 
1   4  987 
    session_id product_id 
0   2  654 
    session_id product_id 
0   3  321 
+0

同樣,我可以定義e對所有leaf_category id的 – Shubham

+0

獲取錯誤的功能相同** TypeError:序列項0:預期的字符串,找到numpy.int64 ** – Shubham

+0

什麼是df.dtypes? – jezrael