Python:如何从熊猫数据框架中的细胞的子选择中减去值?
我尝试从熊猫数据框中的单元子选择中减去值(50)。我想从'rt'中减去 subj == 1 和 cond == std 的值。我想执行此计算 ,以使其他值保持不变。
假设我有以下数据框架:
data = {'subj': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,],
'rt': [100, 102, 101, 100, 101, 101, 105, 105, 106, 104, 104, 106],
'cond':['nov', 'std', 'std', 'emo', 'std', 'emo', 'nov', 'std', 'std',
'emo', 'std', 'emo']}
df = pd.DataFrame(data)
subj rt cond
0 1 100 nov
1 1 102 std
2 1 101 std
3 1 100 emo
4 1 101 std
5 1 101 emo
6 2 105 nov
7 2 105 std
8 2 106 std
9 2 104 emo
10 2 104 std
11 2 106 emo
现在我想从满足标准 subj == 1 和 cond == std 的“ RT”中减去值50。我正在使用以下代码来减去值。
df['rt'] = df[(df['subj'] == 1) & (df['cond'] == 'std')]['rt'].subtract(50)
这就是我期望的:
subj rt cond
0 1 100 nov
1 1 52 std
2 1 51 std
3 1 100 emo
4 1 51 std
5 1 101 emo
6 2 105 nov
7 2 105 std
8 2 106 std
9 2 104 emo
10 2 104 std
11 2 106 emo
相反,这是我得到的:
subj rt cond
0 1 NaN nov
1 1 2.0 std
2 1 1.0 std
3 1 NaN emo
4 1 1.0 std
5 1 NaN emo
6 2 NaN nov
7 2 NaN std
8 2 NaN std
9 2 NaN emo
10 2 NaN std
11 2 NaN emo
我如何保持RT列的其余值而不是拥有NAN?我想以相同的方式将其他RT值减去其他值,而不会为每个条件创建单独的数据帧。
I try to subtract a value (50) from a subselection of cells in a pandas DataFrame. I want to subtract the value from ‘rt’ where subj == 1 and cond == std. I would like to perform this calculation in place so that the other values stay untouched.
Lets say I have the following DataFrame:
data = {'subj': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,],
'rt': [100, 102, 101, 100, 101, 101, 105, 105, 106, 104, 104, 106],
'cond':['nov', 'std', 'std', 'emo', 'std', 'emo', 'nov', 'std', 'std',
'emo', 'std', 'emo']}
df = pd.DataFrame(data)
subj rt cond
0 1 100 nov
1 1 102 std
2 1 101 std
3 1 100 emo
4 1 101 std
5 1 101 emo
6 2 105 nov
7 2 105 std
8 2 106 std
9 2 104 emo
10 2 104 std
11 2 106 emo
Now I want to subtract the value 50 from the 'rt' that meet the criteria subj == 1 and cond == std. I am using the following code to subtract the value.
df['rt'] = df[(df['subj'] == 1) & (df['cond'] == 'std')]['rt'].subtract(50)
This is, what I am expecting:
subj rt cond
0 1 100 nov
1 1 52 std
2 1 51 std
3 1 100 emo
4 1 51 std
5 1 101 emo
6 2 105 nov
7 2 105 std
8 2 106 std
9 2 104 emo
10 2 104 std
11 2 106 emo
Instead this is what i get:
subj rt cond
0 1 NaN nov
1 1 2.0 std
2 1 1.0 std
3 1 NaN emo
4 1 1.0 std
5 1 NaN emo
6 2 NaN nov
7 2 NaN std
8 2 NaN std
9 2 NaN emo
10 2 NaN std
11 2 NaN emo
How can I keep the remaining values of the rt column instead of having NaN? I want to subtract the other rt values by other values in the same manner without creating separate DataFrames for each condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用有效地执行(仅计算3个匹配值):
输出:
Use slicing with
loc
to do it in place efficiently (only the 3 matching values will be computed):output:
这可以使用
np.where()
来完成This can be accomplished using
np.where()
我尝试使用以下DF.LOC,并且可以使用:
I tried using df.loc as below and it worked: