我正在寻找一种总结价值观的方法>或者<给定列中的某个阈值(此处为>6天\u安装到\u事件列).

我try 了很多不同的方法,比如loc、query或groupby,但它只返回值>6不是那些<6.

以下是我try 过的一些事情:

df = pd.DataFrame({
                    'custom_action' : ['First_puchase', 'First_puchase', 'First_puchase', 'First_puchase',
                    'First_puchase', 'First_puchase', 'First_puchase', 'First_puchase'],
                    'days_install_to_event' : [1, 2, 3, 4, 5, 6, 7, 8],
                    'number_unique_users' : [1350, 250, 13, 2, 1, 2, 1, 2]})
df

custom_action days_install_to_event number_unique_users
0 First_puchase                     1                1350
1 First_puchase                     2                 250
2 First_puchase                     3                  13
3 First_puchase                     4                   2
4 First_puchase                     5                   1
5 First_puchase                     6                   2
6 First_puchase                     7                   1
7 First_puchase                     8                   2
8 First_puchase                     9                   3
9 First_puchase                     10                  2

df_1 = df.loc[df['days_install_to_event'] > 6].sum()

df_2 = df.query("days_install_to_event > 6")['number_unique_users'].sum()

df_1
df_2

Output:

custom_action            First_puchaseFirst_puchase
days_install_to_event                            34
number_unique_users                               8
8

Desired output:

custom_action days_install_to_event number_unique_users
0 First_puchase                     1                1350
1 First_puchase                     2                 250
2 First_puchase                     3                  13
3 First_puchase                     4                   2
4 First_puchase                     5                   1
5 First_puchase                     6                   2
6 First_puchase                     7+                  8

在此之前,如果有人问了一个非常类似的问题,我很抱歉.我在过go 的两天里一直在四处寻找,但没有发现任何与我想要的完全匹配的东西.这可能是由于配方.

谢谢你的帮助:)

推荐答案

据我所知,没有现成的解决方案,但您可以通过创建一个helper grouper列来获得这个结果:

# Set days_install_to_event = 7+ if the value is larger than 6
grouper = df['days_install_to_event'].mask(df['days_install_to_event'] > 6, '7+')

然后,在本专栏的帮助下,您可以使用groupby.agg:

In [27]: df.groupby(grouper).agg({
             'number_unique_users': 'sum', 
             'custom_action': 'first',
         }).reset_index()
Out[27]:
  days_install_to_event  number_unique_users  custom_action
0                     1                 1350  First_puchase
1                     2                  250  First_puchase
2                     3                   13  First_puchase
3                     4                    2  First_puchase
4                     5                    1  First_puchase
5                     6                    2  First_puchase
6                    7+                    8  First_puchase

Python-3.x相关问答推荐

只有在Chrome尚未打开的情况下,打开Chrome后,PySimpleGUI窗口才会崩溃

无法导入名称';核心';来自部分初始化的模块';tensorflow_datasets';(很可能是由于循环导入)

类变量的Python子类被视为类方法

如何使用 Selenium Python 连续单击一个按钮直到另一个元素出现?

txt 文件与不同的分隔符到整数列表

使用 RANSAC 在激光雷达点云中查找电力线

如何在 on_ready 事件中使用 change_presence? (discord.py)

为什么 Multiprocessing 的 Lock 不会阻止其他进程使用对象?

正则表达式:匹配字符串中的分隔符(字母和特殊字符)以形成新的子字符串

ImportError:没有名为资源的模块

TypeError:JSON 对象必须是 str,而不是 'dict'

Python过滤器函数 - 单个结果

如何使用 d.items() 更改 for 循环中的所有字典键?

Python configparser 不会接受没有值的键

ImportError:无法在 PyQt5 中导入名称QStringList

如何在 Selenium 和 Python 中使用类型查找元素

用于 unicode 大写单词的 Python 正则表达式

当默认 pip 为 pip2 时,升级 pip3 的正确格式是什么?

TypeError:只有整数标量数组可以转换为标量索引

如何强制 Sphinx 使用 Python 3.x 解释器