gpt4 book ai didi

python - python pandas中是否有countifs函数

转载 作者:行者123 更新时间:2023-12-04 20:51:22 25 4
gpt4 key购买 nike

我有一个数据框

df.head(9)

+------------+-------------+-------------+------------+---------+-------------+-------+--------+------------------+
| session_id | Enter | Exit | Difference | User_id | date | Buyer | Seller | Non_buyer_seller |
+------------+-------------+-------------+------------+---------+-------------+-------+--------+------------------+
| a | 43770 | 43770 | 0:00:00 | 1 | 01/Nov/2019 | 1 | 0 | 0 |
| b | 43770.79991 | 43770.79994 | 0:00:02 | 2 | 01/Nov/2019 | 1 | 0 | 0 |
| c | 43770.5634 | 43770.56351 | 0:00:09 | 3 | 01/Nov/2019 | 0 | 0 | 1 |
| d | 43770.5525 | 43770.5528 | 0:00:25 | 4 | 01/Nov/2019 | 1 | 0 | 0 |
| e | 43770.33724 | 43770.33726 | 0:00:01 | 4 | 01/Nov/2019 | 1 | 0 | 0 |
| f | 43770.65617 | 43770.65623 | 0:00:05 | 5 | 01/Nov/2019 | 0 | 0 | 1 |
| g | 43770.54055 | 43770.54093 | 0:00:32 | 6 | 01/Nov/2019 | 0 | 0 | 1 |
| h | 43770.54203 | 43770.54281 | 0:01:07 | 7 | 01/Nov/2019 | 0 | 0 | 1 |
| i | 43770.64442 | 43770.64478 | 0:00:31 | 8 | 01/Nov/2019 | 0 | 1 | 0 |
+------------+-------------+-------------+------------+---------+-------------+-------+--------+------------------+

我曾经在 excels 中使用 countifs 来计算这样的 session
Buyers_0-to-1 min : =COUNTIFS($G:$G,1, $D$2:$D$671746,">=00:00:00",$D$2:$D$671746,"<=00:01:00")
Buyers_1.1-to-5 min : =COUNTIFS($G:$G,1, $D$2:$D$671746,">=00:01:01",$D$2:$D$671746,"<=00:05:00")

Sellers_0-to-1 min : =COUNTIFS($H:$H,1, $D$2:$D$671746,">=00:00:00",$D$2:$D$671746,"<=00:01:00")
Non_buyer_sellers_0to-1 min : =COUNTIFS($I:$I,1, $D$2:$D$671746,">=00:00:00",$D$2:$D$671746,"<=00:01:00")

那么如何在 python 中为我的数据框做同样的事情。

提前致谢

最佳答案

要执行您列出的第一个 COUNTIF,您可以试试这个。

import pandas as pd

d = {'Difference': ["00:00:00", "00:00:02", "00:00:09", "00:00:25", "00:00:01", "00:00:05", "00:00:32", "00:01:07", "00:00:31"]}
df = pd.DataFrame(data=d)

# 'Difference' column to timedelta
df['Difference'] = pd.to_timedelta(df['Difference'])

# Conditional sum statement
df[(df.Difference >= "00:00:00") & (df.Difference <= "00:01:00")].sum()

Difference 00:01:45
dtype: timedelta64[ns]

我假设时间存储为字符串,并在执行求和之前将它们转换为 timedelta。

注意 sum 包含条件,很像 excel 中的 COUNTIF 语句。您可以修改语句以添加更多条件,或调整列出的条件以获得您正在寻找的任何输出。

关于python - python pandas中是否有countifs函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com