我有一个代表顾客登记(访问)餐厅的数据框。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
提交
取消