我正在循环中的dataframe中生成新列,其中每个循环中的列名都是动态设置的,如果可能的话,我希望使用dataframe.eval()。例如:
for i in roll_windows_values:
readFromColName = 'Movement _' + str(i)
newColName = readFromColName + '_deviation'
df[newColName] = df.eval('readFromColName.mean() - readFromColName')
这给了我一个错误
pandas.core.computation.ops.UndefinedVariableError: name 'readFromColName' is not defined
我还尝试将列名视为变量:
df[newColName] = df.eval('@readFromColName.mean() - @readFromColName')
但这给了我一个错误:
AttributeError: 'Load' object has no attribute '__name__'
有人知道如何从Dataframe.eval中的字符串变量中提取列名吗
如果需要
eval
使用动态列名,可以相应地使用pd.eval
执行此操作:请注意,顶级
eval
调用不需要@variable
注释为什么不使用f字串呢
相关问题 更多 >
编程相关推荐