0 1 2
0 10 20 30
1 40 NaN 60
2 50 55 90
3 60 NaN 80
4 70 75 90
What I need to do is replace every NaN value with 30 , 65 respectively. That means ten added to previous value
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)))
" can concatenate only str(not int) to str. Then i tried your your second code. it worked