[英]Sort and rank a dataframe by grouping each unique user and adding a visit number column for each unique date Python
I am fairly new to Python and I am trying to add a visit number column in a list of users from my data frame.我是 Python 的新手,我正在尝试在我的数据框中的用户列表中添加一个访问编号列。
I have a dataframe of 55 unique users specified by the 'UserID' column and a datetime column indicated by 'dateStarted' I want to create a column that for each user and each unique date there is aa visit number.我有一个 dataframe 的 55 个唯一用户,由“UserID”列和一个由“dateStarted”指示的日期时间列我想创建一个列,为每个用户和每个唯一日期都有一个访问编号。 The dataframe should look like:
dataframe 应如下所示:
UserID dateStarted visit
0 a 01/01/10 1
1 a 01/01/10 1
2 a 01/04/10 2
3 b 06/02/08 1
4 b 07/15/12 2
5 c 02/12/12 1
6 d 02/12/12 1
Group the dataframe by UserID
and rank
the values in dateStarted
to assign ordinal values.按
UserID
对 dataframe 进行分组,并对rank
中的值进行dateStarted
以分配序数值。
df['visit'] = df.groupby('UserID')['dateStarted'].rank(method='dense').astype(int)
UserID dateStarted visit
0 a 01/01/10 1
1 a 01/01/10 1
2 a 01/04/10 2
3 b 06/02/08 1
4 b 07/15/12 2
5 c 02/12/12 1
6 d 02/12/12 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.