Select only numeric columns by DataFrame.select_dtypes
and convert them to absolute values:
cols = df.select_dtypes(np.number).columns
df[cols] = df[cols].abs()
print (df)
cashflow date changeinvalue
index
0 5000 2019-12-31 9300
1 4000 2019-12-31 4000
2 2000 2019-12-31 9000
Your solution failed, because need test elementwise, not by columns in apply
.
So if need test each value separately use DataFrame.applymap
, then test numeric int
and float
for absolute values and all another values not change (so return back):
df = df.applymap(lambda x: abs(x) if isinstance(x, (int, float)) else x)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…