我正在寻找一种总结价值观的方法>或者<给定列中的某个阈值(此处为>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相关问答推荐

"安装serial vs安装psyserial header,"""

如何使用TensorFlow Keras子类化来构建和训练模型

AddMultplicationEquality() 用于多个变量

与 pandas 0.22 相比,pandas 2.0.3 中的 df.replace() 会抛出 ValueError 错误

Django 模型类方法使用错误的 `self`

如何将日期时间索引写入日期类型的表?

生成具有偶数个 0 和 1 的给定长度的所有二进制数

它们是否同样存储在python3的内存中?

在不改变 python 中原始数组顺序的情况下,对多维字符串数组进行降序排序?

以编程方式映射 uniprot ID 时如何解决 400 客户端错误?

用于 BIG 数组计算的多处理池映射比预期的要慢

将名字转换成姓氏、首字母和中间字母的格式

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

python total_ordering:为什么使用 __lt__ 和 __eq__ 而不是 __le__?

str.format_map(mapping) 和 str.format 有什么区别

Python 解包运算符 (*)

无 Python 错误/错误?

清除 PyCharm 运行窗口

尾部斜杠的 FastAPI 重定向返回非 ssl 链接

print(... sep='', '\t' ) 是什么意思?