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

发布于 2025-01-10 05:37:40 字数 305 浏览 0 评论 0原文

我需要计算“负面情绪 - 平均值”列中 2 个单元格之间的差异分数,“participant_id”列和“session”列中的值相同。差值是 block=neg 减去 block=neu 我的预期输出显示在“difference_score”列中

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

预先感谢!

I need to calculate a difference score between 2 cells in the columns 'Negative Emotions - Mean', with the same values in the columns 'participant_id' and 'session'. the difference score is block=neg minus block=neu
My expected output is presented in the column 'difference_score'

How can I do it pandas, without building a dictionary?

Thank in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趴在窗边数星星i 2025-01-17 05:37:40

简单的方法是将 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()

没有提供输出,因为数据是图像。

Over simple way would be to set the ID columns as index and using a mask:

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()

No output provided as the data was an image.

雨的味道风的声音 2025-01-17 05:37:40

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

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

因此,首先,让我们制定您想要执行的逻辑,首先,您想要按participant_id和会话进行分组,然后您想要获取neg的值和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)

One way to do this is to leverage pandas' pandas.DataFrame.groupby and pandas.DataFrame.groupby.GroupBy.apply functions.

Groupby groups your DataFrame based on the specified columns, then apply runs whatever function you pass through over the GroupBy object.

So, first, let's formulate the logic you'd like to do, first, you want to groupby the participant_id and the session, then you'd like to get the value for the neg, and the value for the neu, and then place this difference into a new column named 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文