我正在使用此代碼來獲取分類列的模式:
df.groupby('user_id')['product'].agg(pd.Series.mode).reset_index().rename(columns = {'product': 'most_used_product'}).astype(str)
運行上述代碼后,此列下的值以每個用戶的串列形式出現,例如:
['hat' 'shirt' 'shoes']
['hat' 'shoes']
['shirt']
當我嘗試使用以下方法選擇列的值時:
df[df['most_used_product']== "['hat' 'shirt' 'shoes']"
我得到 SyntaxError: invalid syntax。如何選擇列的值?
uj5u.com熱心網友回復:
您有兩個錯誤,首先是結束方括號,其次您的資料型別不是字串,請使用:
df[df['most_used_product'].astype(str)== "['hat', 'shirt', 'shoes']"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474500.html
上一篇:獲取資料框中特定列每小時的平均值
