後一組的專欄中,我有一個簡單的熊貓數據框命名purchase_cat_df的名字:大熊貓,按功能
email cat
0 [email protected] Mobiles & Tablets
1 [email protected] Mobiles & Tablets
2 [email protected] Mobiles & Tablets
3 [email protected] Mobiles & Tablets
4 [email protected] Home & Living
5 [email protected] Home & Living
我被「電子郵件」和分組,並把「貓」在列表中這樣:
test = purchase_cat_df.groupby('email').apply(lambda x: list(x.cat))
但後來我的數據幀的測試是:
email
[email protected] [Mobiles & Tablets, Mobiles & Tablets, Home & ...
[email protected] [Mobiles & Tablets]
[email protected] [Mobiles & Tablets, Home & Living]
我失去了指數法和名字,我怎麼能評爲第2列?
我想你會得到一個系列,而不是一個DataFrame。 – BrenBarn 2014-09-23 18:45:59
我不確定你的目標是什麼,但我建議調用'purchase_cat_df = purchase_cat_df.set_index('email')'然後你可以通過調用'purchase_cat_df.loc [emailX,'cat']來獲得你的列表。沒有tolist()的tolist()'調用返回一個系列 – ZJS 2014-09-23 19:07:28
顯然,索引不再有意義,因爲每條輸出行都是由具有不同索引的多條輸入行生成的。 – mdurant 2014-09-23 19:49:08