基于熊猫中的numpy选择列来添加列
尝试基于以下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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
np.Select
的结果直接分配给新列You can assign the result of
np.select
to new column directly