Python np.where函数:它只能从特定列检索值吗?
我正在使用np.where()函数来检索实验特定条件下的精度值。我有一个包含很多列的数据框,包括一个精度框(标题为“correct”)和一个反应时间框(标题为“reactiontime”)。我已使用该函数检索到精确数据;我的代码是:Python np.where函数:它只能从特定列检索值吗?,python,numpy,dataframe,np,Python,Numpy,Dataframe,Np,我正在使用np.where()函数来检索实验特定条件下的精度值。我有一个包含很多列的数据框,包括一个精度框(标题为“correct”)和一个反应时间框(标题为“reactiontime”)。我已使用该函数检索到精确数据;我的代码是: df['acc1_eyeheight_f'] = np.where((df.metadata == 'eyeheight_f') & (df.frontal_1.str.contains("1")) | (df.frontal_2.str.contains(
df['acc1_eyeheight_f'] = np.where((df.metadata == 'eyeheight_f') & (df.frontal_1.str.contains("1")) | (df.frontal_2.str.contains("1")), df.correct, np.NaN) #if metadata had the condition, return the corresponding correct value
acc1_eyeheight_f= df.groupby('participant_private_id').mean()['acc1_eyeheight_f']
print(acc1_eyeheight_f)
但是,当我尝试进行完全相同的操作时,通过以下方式检索反应时间数据:
df['rt1_eyeheight_f'] = np.where((df.metadata == 'eyeheight_f') & (df.frontal_1.str.contains("1")) | (df.frontal_2.str.contains("1")), df.reactiontime, np.NaN) #if metadata had the condition, return the corresponding correct value
rt1_eyeheight_f= df.groupby('participant_private_id').mean()['rt1_eyeheight_f']
print(rt1_eyeheight_f)
我得到一个“rt1\u眼高\u f”的关键错误。当我打印(df)时,列确实存在,并且在错误的位置没有空格
我想知道这是否与单元格内的值有关
提前谢谢-我很困惑
[]
其中
没有任何特殊功能。它只使用您提供的数组和值作为参数。发布有关产生异常的代码的问题时,请始终包含完整的回溯-复制并粘贴它,然后将其格式化为代码(选择它并键入ctrl-k
)。另外,请将足够多的df.head()
作为您的文档的一部分。也许可以将该行拆分为两行:创建groupby并将其分配给一个名称;然后提取列并将其指定给其他名称。然后你可以检查groupby,也许可以发布一些groupby的最小示例。你可以共享几行数据吗?我已经添加了一些数据-谢谢。