我有在 Pandas 中打开一个csv文件以及正在创建的新文件的代码。我需要创建一行,以减去两行的绝对值创建“最后两行注释掉”。我已经尝试过很多想法,但都带来了错误。
import pandas as pd
import numpy as np
df = pd.read_csv(filename_read)
ids = df['id']
oosDF = pd.DataFrame()
oosDF['id'] = ids
oosDF['pred'] = pred
oosDF['y'] = df['target']
#oosDF['diff'] = oosdF['pred'] - oosDF['y']
#oosDF['diff'] = oosDF.abs()
最佳答案
我认为您需要按子集输入新的DataFrame
(双[]
中的列名称),然后获取列差的 abs
值:
oosDF = df[['id','pred', 'target']].replace(columns={'target':'y'})
oosDF['diff'] = (oosDF['pred'] - oosDF['y']).abs()
关于python - Pandas :减去两列并将结果保存为绝对值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48867749/