izmir = pd.read_excel(filepath)
izmir_lim = izmir[['Gender','Age','MC_OLD_M>=60','MC_OLD_F>=60',
'MC_OLD_M>18','MC_OLD_F>18','MC_OLD_18>M>5',
'MC_OLD_18>F>5','MC_OLD_M_Child<5','MC_OLD_F_Child<5',
'MC_OLD_M>0<=1','MC_OLD_F>0<=1','Date to Delivery',
'Date to insert','Date of Entery']]
izmir_lim
is a view/copy of izmir
. You subsequently attempt to assign to it. This is what is throwing the error. Use this instead:
izmir_lim = izmir[['Gender','Age','MC_OLD_M>=60','MC_OLD_F>=60',
'MC_OLD_M>18','MC_OLD_F>18','MC_OLD_18>M>5',
'MC_OLD_18>F>5','MC_OLD_M_Child<5','MC_OLD_F_Child<5',
'MC_OLD_M>0<=1','MC_OLD_F>0<=1','Date to Delivery',
'Date to insert','Date of Entery']].copy()
Whenever you ‘create’ a new dataframe from another in the following fashion:
new_df = old_df[list_of_columns_names]
new_df
will have a truthy value in it’s is_copy
attribute. When you attempt to assign to it, pandas throws the SettingWithCopyWarning
.
new_df.iloc[0, 0] = 1 # Should throw an error
You can overcome this in several ways.
Option #1
new_df = old_df[list_of_columns_names].copy()
Option #2 (as @ayhan suggested in comments)
new_df = old_df[list_of_columns_names]
new_df.is_copy = None
Option #3
new_df = old_df.loc[:, list_of_columns_names]