Pandas : 反向累计和的过程
Pandas : reverse cumulative sum's process
我有一个简单的 pandas' 系列,就像这个 :
st
0 74
1 91
2 105
3 121
4 136
5 157
这个系列的数据是累积总和的结果,所以我想知道 pandas 函数是否可以“撤消”这个过程,return 一个新的系列,比如 :
st result
0 74 74
1 91 17
2 105 14
3 121 16
4 136 15
5 157 21
result[0] = st[0], but after result[i] = st[i]-st[i-1].
好像很简单(可能我漏了一个post),但是我什么都没找到...
使用 Series.diff
with replace first missing value by original by Series.fillna
,然后在必要时转换为整数:
df['res'] = df['st'].diff().fillna(df['st']).astype(int)
print (df)
st res
0 74 74
1 91 17
2 105 14
3 121 16
4 136 15
5 157 21
我有一个简单的 pandas' 系列,就像这个 :
st
0 74
1 91
2 105
3 121
4 136
5 157
这个系列的数据是累积总和的结果,所以我想知道 pandas 函数是否可以“撤消”这个过程,return 一个新的系列,比如 :
st result
0 74 74
1 91 17
2 105 14
3 121 16
4 136 15
5 157 21
result[0] = st[0], but after result[i] = st[i]-st[i-1].
好像很简单(可能我漏了一个post),但是我什么都没找到...
使用 Series.diff
with replace first missing value by original by Series.fillna
,然后在必要时转换为整数:
df['res'] = df['st'].diff().fillna(df['st']).astype(int)
print (df)
st res
0 74 74
1 91 17
2 105 14
3 121 16
4 136 15
5 157 21