python - 使用 GroupBy 创建条件列

标签 python pandas dataframe pandas-groupby

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

我尝试使用 np.where 和 pandas pd.groupby 在数据框中创建一个 Status 列,我在其中检查列中的下一个值是否大于当前值,基于对每个 Sensor_ID 和基于此,我尝试分配 Status 是设置为 reset 还是 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

最佳答案

您需要在分组 IOW 之后应用条件,将 groupby 的结果与 np.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

关于python - 使用 GroupBy 创建条件列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56463478/

相关文章:

python - 标记重复行并为重复行添加范围编号,Python 3.6

python - 以编程方式使用的良好文档标准是什么?

python - 发生连接错误时Python Redis中的UnicodeDecodeError

python - 如何重新排列数据框中的行并获得与 pandas 中其他两列具有百分比差异的新列?

python - 选择特定列以计算 Pandas 中的行式总计

python - 如何在数据框中的列之间进行匹配并保留另一列

python - Django:使用 FormView 时可能有多种形式?

python - 为什么 .loc 在 pandas 中切片包含停止,这与典型的 python 切片相反?

python - 有没有一种简单的方法可以将许多新列广播到 Pandas DataFrame 中?

Python:创建空的 pandas 数据框并动态地将元素添加到其列中