如何计算同一列的两个值的差并添加结果新列

时间:2019-05-10 19:44:40

标签: pandas apply

在Excel列A中,值为[1,2,4,4,5,5]。答案是使用新列B:B2 = A2-A1 = 1,B3 = A3-A2 = 2,B4 = A4-A3 = 0我不介意新列的第一行得到0

df = pd.DataFrame({'a':[6,2,4,5], 'b':[3,4,8,7]})

#Columnn c based off b 
df['c] = [0,1,4,-1]

# The first value is a zero by default in the new column 

不起作用

df['c'] = df.apply(lambda x: x.d[n+1] - x.d[n], axis=1)

#Columnn c based off b from above
df['c] = [0,1,4,-1]

# The first value is a zero by default in the new column 

0 个答案:

没有答案