I am trying to transform the negative values under the 'Age' column in my dataset to positive values. So if Age = -15, then Age_new = +15, else if Age >=0, then Age_new remains as Age.
My orginal dataframe is called df_no_mv.
So I have the following code:
def tran_neg_to_pos(df):
if df['Age'] < 0:
return df['Age'] * (-1)
elif df['Age'] >0:
return df['Age']
#create Age_new
df_no_mv['Age_new']=df_no_mv.apply(tran_neg_to_pos,axis=1)
df_no_mv
I see that a new column Age_new is successfully created according to above logic. However I get this warning message:
C:\Users\Admin\anaconda3\lib\site-packages\ipykernel_launcher.py:20: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
How can I fix this?