我想计算time_spent_ratio
(给定customer_id
和给定
month
的手机与平板电脑所花费的时间,我在手机上将time_spent
除以{{1 }}在平板电脑上。
主表
time_spent
输出表
date_month. customer_id. device_name time_spent
01-01-19 2 phone 140
01-01-19 2 tablet 232
01-02-19 2 phone 159
01-02-19 2 tablet 210
01-03-19 2 phone 193
01-03-19 2 tablet 190
答案 0 :(得分:1)
在创建比率列之前,您需要先创建数据透视表。 考虑到您的主要DataFrame是df-
df_pivot = pd.pivot_table(df, index=['date_month','customer_id'], columns='device_name', values='time_spent') #Pivoting on required columns(date and customer_id)
df_flattened = pd.DataFrame(df_pivot.to_records()) #Flatten the pivot table to get a datafrmae
df_flattened['time_spent_ratio'] = df_flattened['phone']/df_flattened['tablet'] #Creating the ratio column
答案 1 :(得分:0)
解决此问题的一种方法是将除time_spent
以外的所有其他列设置为索引,然后将手机的行除以平板电脑的行。
#set all other columns except time_spent as index
res = df.set_index(["device_name","date_month.","customer_id."])
#divide phone rows by tablet rows
res.loc['phone'].div(res.loc['tablet'])
time_spent
date_month. customer_id.
01-01-19 2 0.603448
01-02-19 2 0.757143
01-03-19 2 1.015789