pandas - 如何计算同一列的两个值的差异并使用结果添加新列
问题描述
在具有值 [1,2,4,4,5,5] 的 Excel 列 A 中。答案是新列 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
解决方案
推荐阅读
- javascript - 为什么我从这个函数中获得挂起
- python - Python script throws ValueError while plotting data
- excel - Conditional formatting won't update without double clicking cell in target range
- windows - how to make kubernetes to run in docker for desktop in windows
- python - 为什么“和”排除了其中包含 0 的任何对 (x,y),而不仅仅是排除了两者都是 0 的对?
- node.js - 修改和添加数据不是异步的
- python - 在数据框中提取部分字符串
- swift - Swift 4.2“错误”方法失败:对象中的键无效。为什么?
- javascript - 我们可以将两个函数作为参数传递给另一个函数吗?
- swift - 用户界面:在 Swift 中执行 { }