python - 使用多个条件从 pandas 数据框中选择值

标签 python pandas

我在 Pandas 中有以下数据框。将计算 Score 和 Date_of_interest 列。下面已经填写完毕,以便于解释问题。

首先,我们假设 Score 和 Date_of_interest 列仅填充 NaN。以下是填写其中值的步骤。

a) 我们正在尝试根据下面描述的一个 PC_id 的标准来获取一个感兴趣的日期,例如。 PC_id 200有1998-04-10 02:25:00等等。

b) 为了解决这个问题,我们采用 PC_id 列并检查每一行以查找 Item_id 的变化,每行的得分为 1。对于相同的 Item_id,如第一行和第二行,有 1 和 1,因此值从 1 开始,但在第二行中不变。

c) 在移动和计算第二行的分数时,它还会检查日期时间差异,如果前一个超过 24 小时,则将其删除,分数重置为 1,并且光标移动到第三行。

d) 当分数达到 2 时,我们就达到了第 5 行(索引 4)中的合格分数,我们将相应的日期时间复制到 Date_of_interest 列中。

e) 我们为新的 PC_id 开始新的周期,如第六行所示。

   Datetime        Item_id     PC_id       Value     Score    Date_of_interest

0   1998-04-8 01:00:00   1      200          35         1       NaN
1   1998-04-8 02:00:00   1      200          92         1       NaN
2   1998-04-10 02:00:00  2      200          35         1       NaN
3   1998-04-10 02:15:00  2      200          92         1       NaN
4   1998-04-10 02:25:00  3      200          92         2     1998-04-10 02:25:00

5   1998-04-10 03:00:00  1      201          93         1       NaN
6   1998-04-12 03:30:00  3      201          94         1       NaN
7   1998-04-12 04:00:00  4      201          95         2       NaN
8   1998-04-12 04:00:00  4      201          26         2     1998-04-12 04:00:00
9   1998-04-12 04:30:00  2      201          98         3       NaN

10  1998-04-12 04:50:00  1      202         100         1       NaN
11  1998-04-15 05:00:00  4      202         100         1       NaN
12  1998-04-15 05:15:00  3      202         100         2   1998-04-15 05:15:00
13  1998-04-15 05:30:00  2      202         100         3       NaN
14  1998-04-15 06:00:00  3      202         100         NaN     NaN
15  1998-04-15 06:00:00  3      202         222         NaN     NaN

决赛 table 应如下:

    PC_id      Date_of_interest  

0   200       1998-04-10 02:25:00
1   201       1998-04-12 04:00:00
2   202       1998-04-15 05:15:00

感谢您的帮助。

更新:我当前正在处理的代码:

df_merged_unique = df_merged['PC_id'].unique()
score = 0

for i, row in df_merged.iterrows():
    for elem in df_merged_unique:
        first_date = row['Datetime']
        first_item = 0
        if row['PC_id'] == elem:
            if row['Score'] < 2:
                if row['Item_id'] != first_item:
                    if row['Datetime']-first_date <= pd.datetime.timedelta(days=1):
                        score += 1
                        row['Score'] = score
                        first_date = row['Datetime']
                    else:
                        pass
                else:
                    pass
            else:
                row['Date_of_interest'] = row['Datetime']
                break
        else:
            pass

最佳答案

在使用 pandas 时,通常必须诉诸迭代/命令式方法是一个麻烦的迹象。给定数据框

In [111]: df2
Out[111]: 
              Datetime  Item_id  PC_id  Value
0  1998-04-08 01:00:00        1    200     35
1  1998-04-08 02:00:00        1    200     92
2  1998-04-10 02:00:00        2    200     35
3  1998-04-10 02:15:00        2    200     92
4  1998-04-10 02:25:00        3    200     92
5  1998-04-10 03:00:00        1    201     93
6  1998-04-12 03:30:00        3    201     94
7  1998-04-12 04:00:00        4    201     95
8  1998-04-12 04:00:00        4    201     26
9  1998-04-12 04:30:00        2    201     98
10 1998-04-12 04:50:00        1    202    100
11 1998-04-15 05:00:00        4    202    100
12 1998-04-15 05:15:00        3    202    100
13 1998-04-15 05:30:00        2    202    100
14 1998-04-15 06:00:00        3    202    100
15 1998-04-15 06:00:00        3    202    222

您可以首先按PC_id分组

In [112]: the_group = df2.groupby('PC_id')

然后使用 diff() 应用搜索获取 Item_idDatetime 适当更改的行

In [357]: (the_group['Item_id'].diff() != 0) & \
     ...: (the_group['Datetime'].diff() <= timedelta(days=1))
Out[357]: 
0     False
1     False
2     False
3     False
4      True
5     False
6     False
7      True
8     False
9      True
10    False
11    False
12     True
13     True
14     True
15    False
16    False
dtype: bool

然后只取每组中的第一个日期(第一个匹配项)(如果有)

In [341]: df2[(the_group['Item_id'].diff() != 0) &
     ...:     (the_group['Datetime'].diff() <= timedelta(days=1))]\
     ...: .groupby('PC_id').first()['Datetime'].reset_index()
Out[341]: 
   PC_id            Datetime
0    200 1998-04-10 02:25:00
1    201 1998-04-12 04:00:00
2    202 1998-04-15 05:15:00

关于python - 使用多个条件从 pandas 数据框中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196053/

相关文章:

python - 模型中的元类是否应该从对象继承

python - 使用 str.contains 忽略 NaN

python - 解析 Pandas 中的大字符串值

python - 如何将这个数组数组格式化为 pandas 数据框?

python - 时间作为 Python 字典的关键

python - 从 Python 开始 - 练习 8.14 排序算法。这个已经有名字了吗?

python - 为什么系列[0]和系列[0 :1] yield different results?

python - 在for循环中将数据添加到Pandas Dataframe

python - 如何手动对多索引数据框中的列进行排序?

python - 如果存在某些值,如何重新分配具有重复值的列的值?