I am using the following code to add a nuts value into new columns in dfEU dataframe. I came up with this code from the following past pandas .at versus .loc
Is there a way to solve this warning? dfEU was created using the following query:
dfEU = df.query('continent == "EU" & country_id == "Belgium"')
for row in dfEU.itertuples():
lati=float(getattr(row, 'locationlatitude'))
longi=float(getattr(row, 'locationlongitude'))
nuts = nf.find(lat=lati, lon=longi)
if nuts:
dfEU.at[row.Index, 'nuts1'] = nuts[0].get('NUTS_NAME')
dfEU.at[row.Index, 'nuts1id'] = nuts[0].get('FID')
dfEU.at[row.Index, 'nuts2'] = nuts[1].get('NUTS_NAME')
dfEU.at[row.Index, 'nuts2id'] = nuts[1].get('FID')
dfEU.at[row.Index, 'nuts3'] = nuts[2].get('NUTS_NAME')
dfEU.at[row.Index, 'nuts3id'] = nuts[2].get('FID')
else:
dfEU.at[row.Index, 'nuts1'] = 'Nan'
When launching the code I receive the following warning:
C:\Users\win\anaconda3\lib\site-packages\pandas\core\indexing.py:1596: 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
self.obj[key] = _infer_fill_value(value)
C:\Users\win\anaconda3\lib\site-packages\pandas\core\indexing.py:1765: 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
isetter(loc, value)