Pandas - 如何根据其他列值从另一个单元格中减去一个单元格?
我需要计算“负面情绪 - 平均值”列中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的方法是将 ID 列设置为索引并使用掩码:
没有提供输出,因为数据是图像。
Over simple way would be to set the ID columns as index and using a mask:
No output provided as the data was an image.
一种方法是利用 pandas 的
pandas .DataFrame.groupby
和pandas.DataFrame.groupby.GroupBy.apply
函数。Groupby 根据指定的列对 DataFrame 进行分组,然后 apply 运行您通过 GroupBy 对象传递的任何函数。
因此,首先,让我们制定您想要执行的逻辑,首先,您想要按participant_id和会话进行分组,然后您想要获取neg的值和neu的值,然后放置将此差异放入名为 Difference_score 的新列中。
One way to do this is to leverage pandas'
pandas.DataFrame.groupby
andpandas.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.