我想从同一行(轴= 1)的前一列值中减去当前列值,除了第一列值
My Dataframe:
A B C D
0 5 11 4 5
1 3 2 3 4
2 6 4 8 2
3 4 3 5 8
Expected Dataframe:
A B C D
0 5 6 -2 7
1 3 -1 4 0
2 6 -2 10 12
3 4 -1 6 2
答案 0 :(得分:1)
似乎没有cum-diff内置函数
for x, y in enumerate(df.columns):
if x ==0 :
df[y]=df[y]
else :
df[y]=df[y]-df[df.columns[x-1]]
df
Out[494]:
A B C D
0 5 6 -2 7
1 3 -1 4 0
2 6 -2 10 -8
3 4 -1 6 2
def cumdiff(df,axis):
if axis==1 :
for x, y in enumerate(df.columns):
if x == 0:
df[y] = df[y]
else:
df[y] = df[y] - df[df.columns[x - 1]]
return df
else :
for x, y in enumerate(df.index):
if x == 0:
df[y] = df[y]
else:
df[y] = df[y] - df[df.columns[x - 1]]
return df
cumdiff(df,axis=1)
Out[501]:
A B C D
0 5 6 -2 7
1 3 -1 4 0
2 6 -2 10 -8
3 4 -1 6 2
答案 1 :(得分:1)
您只需使用 pandas.DataFrame.expanding 和 reduce function 即可。所以你的代码可以是这样的:
import pandas as pd
from functools import reduce
data = [[5, 11, 4, 5],
[3, 2, 3, 4],
[6, 4, 8, 2],
[4, 3, 5, 8]]
df = pd.DataFrame(data=data, columns=['A','B','C','D'])
print(df)
>>> A B C D
0 5 11 4 5
1 3 2 3 4
2 6 4 8 2
3 4 3 5 8
diff_df = df.expanding(axis=1).apply(lambda x: reduce(lambda a,b : b-a,x)).astype(int)
print(diff_df)
>>> A B C D
0 5 6 -2 7
1 3 -1 4 0
2 6 -2 10 -8
3 4 -1 6 2