Pandas 基于其他现有列进行计算的新列

Pandas new column with calculation based on other existing column

我有一个 Panda,想根据现有列进行计算。 然而,适用。由于某种原因,该功能无法正常工作。

有点像letssay

df = pd.DataFrame({'Age': age, 'Input': input})

并且输入列类似于 [1.10001, 1.49999, 1.60001] 现在我想向 Dataframe 添加一个新列,即执行以下操作:

使用Series.add, Series.mul and Series.astype:

#input is python code word (builtin), so better dont use it like variable
inp = [1.10001, 1.49999, 1.60001] 
age = [10,20,30]
df = pd.DataFrame({'Age': age, 'Input': inp})
df['new'] = df['Input'].add(0.0001).mul(10).astype(int)
print (df)
   Age    Input  new
0   10  1.10001   11
1   20  1.49999   15
2   30  1.60001   16

您可以创建一个简单的函数,然后按行应用它。

def f(row):
    return int((row['input']+0.0001)*10))

df['new'] = df.apply(f, axis=1)