我有一個代表顧客登記(訪問)餐廳的數據框。year簡直就是在餐廳辦理入住的那一年。std_checkin我想要做的是在我的初始數據框中添加一列df,表示每年訪問的標準差。因此,我需要計算每年總訪問次數的標準差。data = { 'restaurant_id': ['--1UhMGODdWsrMastO9DZw', '--1UhMGODdWsrMastO9DZw','--1UhMGODdWsrMastO9DZw','--1UhMGODdWsrMastO9DZw','--1UhMGODdWsrMastO9DZw','--1UhMGODdWsrMastO9DZw','--6MefnULPED_I942VcFNA','--6MefnULPED_I942VcFNA','--6MefnULPED_I942VcFNA','--6MefnULPED_I942VcFNA'], 'year': ['2016','2016','2016','2016','2017','2017','2011','2011','2012','2012'], }df = pd.DataFrame (data, columns = ['restaurant_id','year'])# total number of checkins per restaurantd = df.groupby('restaurant_id')['year'].count().to_dict()df['nb_checkin'] = df['restaurant_id'].map(d)grouped = df.groupby(["restaurant_id"])avg_annual_visits = grouped["year"].count() / grouped["year"].nunique()avg_annual_visits = avg_annual_visits.rename("avg_annual_visits")df = df.merge(avg_annual_visits, left_on="restaurant_id", right_index=True)df.head(10)從這里開始,我不知道如何用 pandas 寫出我想要的內容。如果需要任何澄清,請詢問。謝謝你!
1 回答

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
我想你想做:
counts = df.groupby('restaurant_id')['year'].value_counts()
counts.std(level='restaurant_id')
的輸出counts,即每年每家餐廳的總訪問量:
restaurant_id year
--1UhMGODdWsrMastO9DZw 2016 4
2017 2
--6MefnULPED_I942VcFNA 2011 2
2012 2
Name: year, dtype: int64
并輸出為std
restaurant_id
--1UhMGODdWsrMastO9DZw 1.414214
--6MefnULPED_I942VcFNA 0.000000
Name: year, dtype: float64
添加回答
舉報
0/150
提交
取消