熊猫新列,其计算基于其他现有列

时间:2019-11-29 10:02:17

标签: python pandas calculation

我有一个Panda,想要基于现有列进行计算。 但是,适用。功能由于某些原因无法正常工作。

就像是说说

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

,输入列类似于[1.10001,1.49999,1.60001] 现在,我想向数据框添加一个新列,该操作如下:

  • 向列中的每个元素添加0.0001
  • 每个值乘以10
  • 将新列的每个值转换为int

2 个答案:

答案 0 :(得分:2)

使用Series.addSeries.mulSeries.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

答案 1 :(得分:1)

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

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

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