基于熊猫中的numpy选择列来添加列

发布于 2025-01-24 14:19:06 字数 1838 浏览 0 评论 0原文

尝试基于以下numpy选择语句中的熊猫数据框中添加列,

我可以将值作为一个数据框,

f=pd.DataFrame(np.select(
    [   
        df.groupby('usernumber')['date'].nunique().between(0, 3, inclusive=True), 
        df.groupby('usernumber')['date'].nunique().between(3,5, inclusive=True), 
        df.groupby('usernumber')['date'].nunique()>5
     
    ], 
    [
        
        'Few', 
        'Moderate',
        'Many'
    ], 
    default='Unknown'
),columns = ['UsageType'])

理想情况下显示为数据

df

usernumber  date      UsageType
12314       20220201  Few
12314       20220202  Few
12314       20220203  Few
32423       20220201  Moderate
32423       20220202  Moderate
32423       20220203  Moderate
32423       20220204  Moderate
43535       20220201  Many
43535       20220202  Many
43535       20220203  Many
43535       20220204  Many
43535       20220205  Many

usernumber  date    Role    Task
12314   20220201    IT          logon
12314   20220202    IT          logon
12314   20220203    IT          logon
32423   20220201    DB          logon
32423   20220202    DB          logoff
32423   20220203    DB          logon
32423   20220204    DB          logon
43535   20220201    Admin       logon
43535   20220202    Admin       logon
43535   20220203    Admin       logoff
43535   20220204    Admin       logon
43535   20220205    Admin       logon
31249   20220206    Associate   logon
13151   20220206    Associate   logon
15146   20220201    UX          logon
15146   20220201    UX          logoff
15146   20220202    UX          logon
15146   20220202    UX          logoff
15146   20220203    UX          logon
15146   20220203    UX          logoff
15146   20220204    UX          logon
15146   20220205    UX          logoff
15146   20220205    UX          logon

Trying to add a column in pandas dataframe based on the following numpy select statement

I can get the value as a dataframe shown below

f=pd.DataFrame(np.select(
    [   
        df.groupby('usernumber')['date'].nunique().between(0, 3, inclusive=True), 
        df.groupby('usernumber')['date'].nunique().between(3,5, inclusive=True), 
        df.groupby('usernumber')['date'].nunique()>5
     
    ], 
    [
        
        'Few', 
        'Moderate',
        'Many'
    ], 
    default='Unknown'
),columns = ['UsageType'])

Ideally I would like this to be added as a column with the classified values in the main df

df

usernumber  date      UsageType
12314       20220201  Few
12314       20220202  Few
12314       20220203  Few
32423       20220201  Moderate
32423       20220202  Moderate
32423       20220203  Moderate
32423       20220204  Moderate
43535       20220201  Many
43535       20220202  Many
43535       20220203  Many
43535       20220204  Many
43535       20220205  Many

Sample df data

usernumber  date    Role    Task
12314   20220201    IT          logon
12314   20220202    IT          logon
12314   20220203    IT          logon
32423   20220201    DB          logon
32423   20220202    DB          logoff
32423   20220203    DB          logon
32423   20220204    DB          logon
43535   20220201    Admin       logon
43535   20220202    Admin       logon
43535   20220203    Admin       logoff
43535   20220204    Admin       logon
43535   20220205    Admin       logon
31249   20220206    Associate   logon
13151   20220206    Associate   logon
15146   20220201    UX          logon
15146   20220201    UX          logoff
15146   20220202    UX          logon
15146   20220202    UX          logoff
15146   20220203    UX          logon
15146   20220203    UX          logoff
15146   20220204    UX          logon
15146   20220205    UX          logoff
15146   20220205    UX          logon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2025-01-31 14:19:06

您可以将np.Select的结果直接分配给新列

nunique = df['usernumber'].map(df.groupby('usernumber')['date'].nunique())

df['UsageType'] = np.select(
    [
        nunique.between(0, 3, inclusive=True),
        nunique.between(3, 4, inclusive=True),
        nunique.ge(5)
    ],
    [
        'Few',
        'Moderate',
        'Many'
    ],
    default='Unknown'
)
print(df)

    usernumber      date UsageType
0        12314  20220201       Few
1        12314  20220202       Few
2        12314  20220203       Few
3        32423  20220201  Moderate
4        32423  20220202  Moderate
5        32423  20220203  Moderate
6        32423  20220204  Moderate
7        43535  20220201      Many
8        43535  20220202      Many
9        43535  20220203      Many
10       43535  20220204      Many
11       43535  20220205      Many

You can assign the result of np.select to new column directly

nunique = df['usernumber'].map(df.groupby('usernumber')['date'].nunique())

df['UsageType'] = np.select(
    [
        nunique.between(0, 3, inclusive=True),
        nunique.between(3, 4, inclusive=True),
        nunique.ge(5)
    ],
    [
        'Few',
        'Moderate',
        'Many'
    ],
    default='Unknown'
)
print(df)

    usernumber      date UsageType
0        12314  20220201       Few
1        12314  20220202       Few
2        12314  20220203       Few
3        32423  20220201  Moderate
4        32423  20220202  Moderate
5        32423  20220203  Moderate
6        32423  20220204  Moderate
7        43535  20220201      Many
8        43535  20220202      Many
9        43535  20220203      Many
10       43535  20220204      Many
11       43535  20220205      Many
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文