You can shift
the dataframe and then add 10 , then fillna
with that df:
df = df.fillna(df.shift().add(10))
# for a new df :-> new_df = df.fillna(df.shift().add(10))
print(new_df)
0 1 2
0 10 20.0 30
1 40 30.0 60
2 50 55.0 90
3 60 65.0 80
4 70 75.0 90
Note: If you have both numeric and string columns, you can first select only numeric columns using df.select_dtypes
and then do the operation:
num_df = df.select_dtypes(np.number)
df.loc[:,num_df.columns] = num_df.fillna(num_df.shift().add(10))
If you want to create a new df and not modify the original one then use df.assign
to assign the new values to the subset of columns:
new_df = df.assign(**num_df.fillna(num_df.shift().add(10)))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…