Generation

generate functionTue, 24 Jan 2023

There is a Pandas dataframe: userId movieId rating timestamp 0 1 31 2.5 1260759144 1 1 1029 3.0 1260759179 2 1 1061 3.0 1260759182 3 1 1129 2.0 1260759185 4 1 1172 4.0 1260759205 Calculate the average lifetime of users. The lifetime is the difference between the maximum and minimum values of the "timestamp" column for a given "userId".

import pandas as pd ratings = pd.read_csv('ratings.csv') print(ratings.head()) # Create a function which return the average lifetime of users def lifetime(group): return group.max() - group.min() lifetime_users = ratings.groupby('userId').agg(lifetime) print(lifetime_users) average_lifetime = lifetime_users['timestamp'].mean() print(average_lifetime) # output: # userId movieId rating timestamp # 0 1 31 2.5 1260759144 # 1 1 1029 3.0 1260759179 # 2 1 1061 3.0 1260759182 # 3 1 1129 2.0 1260759185 # 4 1 1172 4.0 1260759205 # timestamp # userId # 1 203560 # 2 866607 # 3 8

Questions about programming?Chat with your personal AI assistant