首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用shift()获取熊猫数据中的下一个元素

用shift()获取熊猫数据中的下一个元素
EN

Stack Overflow用户
提问于 2022-01-25 16:55:27
回答 1查看 83关注 0票数 1

this question上,我有一个类似于这样的数据,我想创建一个新的列next_domainnext_next_domain

它是通过根据时间戳查找IP的下一个域(和下一个域)来计算的。我怎样才能在熊猫身上做到这一点?

输入:

代码语言:javascript
复制
    domain      ip      timestamp
0   Google      101     2020-04-01 23:01:41
1   Google      101     2020-04-01 23:01:59
2   Google      101     2020-04-02 12:01:41
3   Facebook    101     2020-04-02 13:11:33
4   Facebook    101     2020-04-02 13:11:35
5   Youtube     103     2020-04-21 13:01:41
6   Youtube     103     2020-04-21 13:11:46
7   Youtube     103     2020-04-22 01:01:01
8   Google      103     2020-04-22 02:11:23
9   Facebook    103     2020-04-23 14:11:13
10  Youtube     103     2020-04-23 14:11:55

预期输出:

代码语言:javascript
复制
    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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-25 17:13:24

在前面问题的my answer基础上,您可以创建一个函数并迭代该过程:

代码语言:javascript
复制
def next_domain(df, col='domain', group='ip'):
    s = df[col]
    return (s.where(s.ne(s.shift())) # keep only first domain of each stretch
                      .groupby(df[group])                   # per group
                      .apply(lambda s: s.bfill().shift(-1)) # bfill and shift up
                    )

df['next_domain'] = next_domain(df, 'domain')
df['next_next_domain'] = next_domain(df, 'next_domain')

产出:

代码语言:javascript
复制
      domain   ip            timestamp next_domain next_next_domain
0     Google  101  2020-04-01 23:01:41    Facebook              NaN
1     Google  101  2020-04-01 23:01:59    Facebook              NaN
2     Google  101  2020-04-02 12:01:41    Facebook              NaN
3   Facebook  101  2020-04-02 13:11:33         NaN              NaN
4   Facebook  101  2020-04-02 13:11:35         NaN              NaN
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              NaN
10   Youtube  103  2020-04-23 14:11:55         NaN              NaN
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70852633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档