计算 pandas 中组切换的次数

Eis*_*sen 3 python pandas

我有以下数据框:

我想创建一个新列来计算 IP 切换域的​​次数。

输入:

    domain      ip      timestamp              next_domain    next_next_domain
0   Google      101     2020-04-01 23:01:41    Facebook       N/A
1   Google      101     2020-04-01 23:01:59    Facebook       N/A
2   Google      101     2020-04-02 12:01:41    Facebook       N/A
3   Facebook    101     2020-04-02 13:11:33    N/A            N/A
4   Facebook    101     2020-04-02 13:11:35    N/A            N/A
5   Youtube     103     2020-04-21 13:01:41    Google         Facebook
6   Youtube     103     2020-04-21 13:11:46    Google         Facebook
7   Youtube     103     2020-04-22 01:01:01    Google         Facebook
8   Google      103     2020-04-22 02:11:23    Facebook       Youtube
9   Facebook    103     2020-04-23 14:11:13    Youtube        N/A
10  Youtube     103     2020-04-23 14:11:55    N/A            N/A
Run Code Online (Sandbox Code Playgroud)

输出:

    domain      ip      timestamp              next_domain    next_next_domain  switch_count
0   Google      101     2020-04-01 23:01:41    Facebook       N/A               1
1   Google      101     2020-04-01 23:01:59    Facebook       N/A               1
2   Google      101     2020-04-02 12:01:41    Facebook       N/A               1
3   Facebook    101     2020-04-02 13:11:33    N/A            N/A               1
4   Facebook    101     2020-04-02 13:11:35    N/A            N/A               1
5   Youtube     103     2020-04-21 13:01:41    Google         Facebook          3
6   Youtube     103     2020-04-21 13:11:46    Google         Facebook          3
7   Youtube     103     2020-04-22 01:01:01    Google         Facebook          3
8   Google      103     2020-04-22 02:11:23    Facebook       Youtube           3
9   Facebook    103     2020-04-23 14:11:13    Youtube        N/A               3
10  Youtube     103     2020-04-23 14:11:55    N/A            N/A               3
Run Code Online (Sandbox Code Playgroud)

IP 101 切换了 1 次,因为它来自 Google -> Facebook。IP 103 切换了 3 次,因为它来自 Youtube -> Google -> Facebook -> Youtube。

我怎样才能在熊猫中做到这一点?

小智 5

您可以groupby“ip”并转换一个 lambda 来检查连续域名是否不同(请注意,我们只是减去 1,因为我们也计算第一个域名,这不计入“switch”):

df['switch'] = df.groupby('ip')['domain'].transform(lambda x: x.shift().ne(x).sum()-1)
Run Code Online (Sandbox Code Playgroud)

输出:

      domain   ip            timestamp next_domain next_next_domain  switch
0     Google  101  2020-04-01 23:01:41    Facebook              NaN       1
1     Google  101  2020-04-01 23:01:59    Facebook              NaN       1
2     Google  101  2020-04-02 12:01:41    Facebook              NaN       1
3   Facebook  101  2020-04-02 13:11:33         NaN              NaN       1
4   Facebook  101  2020-04-02 13:11:35         NaN              NaN       1
5    Youtube  103  2020-04-21 13:01:41      Google         Facebook       3
6    Youtube  103  2020-04-21 13:11:46      Google         Facebook       3
7    Youtube  103  2020-04-22 01:01:01      Google         Facebook       3
8     Google  103  2020-04-22 02:11:23    Facebook          Youtube       3
9   Facebook  103  2020-04-23 14:11:13     Youtube              NaN       3
10   Youtube  103  2020-04-23 14:11:55         NaN              NaN       3
Run Code Online (Sandbox Code Playgroud)