【发布时间】:2020-09-28 16:15:02
【问题描述】:
我有一个数据框,代表餐馆的顾客签到(访问)。 year 只是发生在餐厅登记的年份。
- 我想做的是在我的初始数据框
df中添加一列average_checkin,它代表餐厅每年的平均访问次数。
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'])
# here i count the total number of checkins a restaurant had
d = df.groupby('restaurant_id')['year'].count().to_dict()
df['nb_checkin'] = df['restaurant_id'].map(d)
mean_checkin= df.groupby(['restaurant_id','year']).agg({'nb_checkin':[np.mean]})
mean_checkin.columns = ['mean_checkin']
mean_checkin.reset_index()
# the values in mean_checkin makes no sens
#I need to merge it with df to add that new column
我还是熊猫库的新手,我尝试过类似的方法,但我的结果毫无意义。我的语法有问题吗?如果需要任何澄清,请询问。
【问题讨论】:
标签: python pandas dataframe jupyter-notebook feature-engineering