Pandas - 如何根据其他列的值从一个单元格中减去另一个单元格?

Pandas - How to substract one cell from another cell based on other columns values?

我需要计算 'Negative Emotions - Mean' 列中 2 个单元格之间的差异分数,其中 'participant_id' 和 'session' 列中的值相同。差异分数是 block=neg 减去 block=neu 我的预期输出显示在 'difference_score'

列中

如何在不构建字典的情况下pandas?

提前致谢!

简单的方法是将 ID 列设置为索引并使用掩码:

df2 = df.set_index(['participant_id', 'session'])

mask = df2['block'].eq('neg')

df2['difference_score'] = df2.loc[mask, 'Negative Emotions - Mean']-df2.loc[~mask, 'Negative Emotions - Mean']

df2.reset_index()

由于数据是图像,因此未提供输出。

一种方法是利用 pandas' pandas.DataFrame.groupby and pandas.DataFrame.groupby.GroupBy.apply 函数。

Groupby 根据指定的列对您的 DataFrame 进行分组,然后 apply 运行您在 GroupBy 对象上传递的任何函数。

所以,首先,让我们制定您想要执行的逻辑,首先,您想要按 participant_id 和会话进行分组,然后您想要获取负数的值,然后neu 的值,然后将此差异放入名为 difference_score.

的新列中
# This function will get the difference from the grouped rows.
def get_score_difference(rows: pd.DataFrame):
    # Get neg value in a try catch block, ensuring neg is defaulted to 0 if not in df
    try:
        neg = rows.loc[rows['block'] == 'neg']['Negative Emotion - Mean'][0]
    except Exception as e:
        neg = 0

    # Get neu value in the same fashion as neg
    try:
        neu = rows.loc[rows['block'] == 'neu']['Negative Emotion - Mean'][0]
    except Exception as e:
        neu = 0

    # Add new column with neg - neu
    rows['difference_score'] = neg - neu

    # Return new rows
    return rows

# Apply the function to the dataframe
df.groupby(['participant_id', 'session']).apply(get_score_difference)