【问题标题】:Create conditional column using GroupBy使用 GroupBy 创建条件列
【发布时间】:2019-06-05 15:22:24
【问题描述】:

我想根据数据框中一列中的分组变量在我的数据框中创建一个新列,然后检查数据框中另一列中的条件。

我尝试使用 np.where 和 pandas pd.groupby 在数据框中创建一个状态列,我正在检查列中的下一个值是否大于基于对每个 Sensor_ID 和基于此,我试图分配状态是否设置为重置或 not_reset,但是我没有成功使用代码。

import pandas as pd
df = pd.DataFrame(data = {'Sensor_ID':['A1', 'A1', 'A1', 'A2','A2', 'A2', 'A2', 'A3', 'A3', 'A3', 'A3', 'A3'], 'Reading':[81, 83.5, 87, 90, 81, 82, 85, 78, 79, 78, 80, 78]})
df

   Sensor_ID  Reading
0         A1     81.0
1         A1     83.5
2         A1     87.0
3         A2     90.0
4         A2     81.0
5         A2     82.0
6         A2     85.0
7         A3     78.0
8         A3     79.0
9         A3     78.0
10        A3     80.0
11        A3     78.0

我想使用 np.where 创建以下条件,但我想使用 Sensor_ID 作为分组变量。

df['Status'] = np.where(df.Reading.shift(-1) > df.Reading, 'not_reset', 'reset')

我将 np.where 与 groupby 和 transform 一起使用

df['Status'] = np.where(df.groupby('Sensor_ID')['Reading'].transform(df['Reading'].shift(-1) > df['Reading'], 'not_reset', 'reset'))

TypeError: 'Series' objects are mutable, thus they cannot be hashed

我也尝试过将 apply 和 transform 与 groupby 一起使用,但出现错误:

df['Status'] = df.groupby('Sensor_ID').apply(lambda row: 'not_reset' if row['Reading'].shift(-1) > row['Reading'] else 'reset')

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). --> As its comparing the whole series.

df['Status'] = df.groupby('Sensor_ID').transform(df['Reading'].shift(-1) > df['Reading'], 'not_reset', 'reset')
TypeError: 'Series' objects are mutable, thus they cannot be hashed

预期输出:

       Sensor_ID  Reading     Status
0             A1     81.0  not_reset
1             A1     83.5  not_reset
2             A1     87.0  not_reset
3             A2     90.0  not_reset
4             A2     81.0      reset
5             A2     82.0  not_reset
6             A2     85.0  not_reset
7             A3     78.0  not_reset
8             A3     79.0  not_reset
9             A3     78.0      reset
10            A3     80.0  not_reset
11            A3     78.0      reset

【问题讨论】:

    标签: python pandas dataframe pandas-groupby


    【解决方案1】:

    您需要在分组 IOW 之后应用条件,使用groupbynp.where 的结果)。

    我会使用groupbydiff,这与比较移位1 的值相同。就这么简单,

    np.where(
        df.groupby('Sensor_ID')['Reading'].diff().fillna(1) > 0, 'not reset', 'reset')
    
    array(['not reset', 'not reset', 'not reset', 'not reset', 'reset',
           'not reset', 'not reset', 'not reset', 'not reset', 'reset',
           'not reset', 'reset'], dtype='<U9')
    

    另请参阅here,了解我针对(现已删除)问题的回答的初始版本。


    df['Status'] = np.where(
        df.groupby('Sensor_ID')['Reading'].diff().fillna(1) > 0, 'not reset', 'reset')
    df
    
       Sensor_ID  Reading     Status
    0         A1     81.0  not reset
    1         A1     83.5  not reset
    2         A1     87.0  not reset
    3         A2     90.0  not reset
    4         A2     81.0      reset
    5         A2     82.0  not reset
    6         A2     85.0  not reset
    7         A3     78.0  not reset
    8         A3     79.0  not reset
    9         A3     78.0      reset
    10        A3     80.0  not reset
    11        A3     78.0      reset
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-11
      • 2021-12-26
      • 1970-01-01
      • 2022-12-15
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多