我有一个数据框,包含用户ID、产品ID、创建时间和删除时间等列。我想添加一个布尔型的“is_switch”列,如果对于给定的用户,创建时间戳在时间间隔(比如一秒)内与该用户组中任何其他行的删除时间相同,则为True。如何在不迭代每一行的情况下完成这个操作?或者说,这是适当的方法吗?
我正在尝试编写一个自定义函数,用于在.apply中运行每个用户组,但我不知道如何一次性比较所有其他行与当前行。
我想添加“is_switch”列,以便输出结果如下:
我正在尝试编写一个自定义函数,用于在.apply中运行每个用户组,但我不知道如何一次性比较所有其他行与当前行。
# Code to create sample data frame.
# the below are just timestamps that are within a second of each other.
import datetime
a = datetime.datetime.now()
a2 = a-datetime.timedelta(seconds=1)
b = datetime.datetime.now()-datetime.timedelta(days=4)
b2 = b-datetime.timedelta(seconds=1)
c = datetime.datetime.now()-datetime.timedelta(days=40)
c2 = c - datetime.timedelta(seconds=1)
d = datetime.datetime.now()-datetime.timedelta(days=30)
d2 = d - datetime.timedelta(seconds=1)
e = datetime.datetime.now()-datetime.timedelta(days=60)
e2 = e - datetime.timedelta(seconds=1)
f = datetime.datetime.now()-datetime.timedelta(days=100)
g = datetime.datetime.now()-datetime.timedelta(days=99)
df = pd.DataFrame(
{"user_id" : [0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4],
"product_id" : [100, 101, 102, 101, 102, 104, 105, 106, 107, 105, 106, 107],
"created_at" : [a, a, b, c, d, c, f, f, e2, f, f, d],
"removed_at" : ['NaT', b2, 'NaT', d2, 'NaT', 'NaT', e, g, 'NaT', e2, g, b]},
index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
df
print(df)
yields this:
user_id product_id created_at removed_at
0 0 100 2019-08-04 09:15:05.200981 NaT
1 1 101 2019-08-04 09:15:05.200981 2019-07-31 09:15:04.201063
2 1 102 2019-07-31 09:15:05.201063 NaT
3 2 101 2019-06-25 09:15:05.201121 2019-07-05 09:15:04.201179
4 2 102 2019-07-05 09:15:05.201179 NaT
5 2 104 2019-06-25 09:15:05.201121 NaT
6 3 105 2019-04-26 09:15:05.201290 2019-06-05 09:15:05.201235
7 3 106 2019-04-26 09:15:05.201290 2019-04-27 09:15:05.201324
8 3 107 2019-06-05 09:15:04.201235 NaT
9 4 105 2019-04-26 09:15:05.201290 2019-06-05 09:15:04.201235
10 4 106 2019-04-26 09:15:05.201290 2019-04-27 09:15:05.201324
11 4 107 2019-07-05 09:15:05.201179 2019-07-31 09:15:05.201063
所以我现在有这样的东西:
group_by_user = df.groupby('user_id')
def calculate_is_switch(grp):
# What goes here? how can i do it without iterating over each row?
# group_by_user.apply(calculate_is_switch)
我想添加“is_switch”列,以便输出结果如下:
user_id product_id created_at removed_at \
0 0 100 2019-08-04 09:15:05.200981 NaT
1 1 101 2019-08-04 09:15:05.200981 2019-07-31 09:15:04.201063
2 1 102 2019-07-31 09:15:05.201063 NaT
3 2 101 2019-06-25 09:15:05.201121 2019-07-05 09:15:04.201179
4 2 102 2019-07-05 09:15:05.201179 NaT
5 2 104 2019-06-25 09:15:05.201121 NaT
6 3 105 2019-04-26 09:15:05.201290 2019-06-05 09:15:05.201235
7 3 106 2019-04-26 09:15:05.201290 2019-04-27 09:15:05.201324
8 3 107 2019-06-05 09:15:04.201235 NaT
9 4 105 2019-04-26 09:15:05.201290 2019-06-05 09:15:04.201235
10 4 106 2019-04-26 09:15:05.201290 2019-04-27 09:15:05.201324
11 4 107 2019-07-05 09:15:05.201179 2019-07-31 09:15:05.201063
is_switch
0 False
1 False
2 True
3 False
4 True
5 False
6 False
7 False
8 True
9 False
10 False
11 False
removed_at
的第一个值(如果存在于该组中)? - jezraeluser_id
中,removed_at
是否可能有多个值? - Erfany = np.any((np.abs(y).astype(np.int64) <= val.value), axis=1)
更改为y = np.any((np.abs(y).astype(np.int64) <= val.value), axis=0)
。 - jezrael