我对数据帧的计数结果是单行,如下所示:
Users Address Email Phone Work
552 343 207 117 57
我想在每列之间插入第二列和上一列之间的差异,以便得到:
Users DiffAdd Address DiffEmail Email DiffPhone Phone DiffWork Work
552 -209 343 -136 207 -90 117 -60 57
或者,如果可能的话,我只需用相应的差异替换每列的值,结果如下:
Users Address Email Phone Work
552 -209 -136 -90 -60
我尝试做后一种情况,以便我用差异替换值,但这会立即将新值应用于下一次计算,所以我得到了没有意义的疯狂值。
谢谢!
答案 0 :(得分:0)
使用:
diff
获取所有列的差异,按add_prefix
concat
列axis=1
[]
iloc
df1 = df.diff(axis=1).add_prefix('Diff')
df = pd.concat([df, df1], axis=1)
c = list(sum(zip(df1.columns, df.columns), ()))
print (c)
['DiffUsers', 'Users', 'DiffAddress', 'Address', 'DiffEmail',
'Email', 'DiffPhone', 'Phone', 'DiffWork', 'Work']
df = df[c].iloc[:, 1:]
print (df)
Users DiffAddress Address DiffEmail Email DiffPhone Phone DiffWork \
0 552 -209.0 343 -136.0 207 -90.0 117 -60.0
Work
0 57
可替换地:
df2 = df.diff(axis=1).fillna({'Users':df['Users']})
print (df2)
Users Address Email Phone Work
0 552.0 -209.0 -136.0 -90.0 -60.0