1

Previously, I'm asking How to update series based on other pandas dataframe, and there exist 3 answers, but 3 of them give caveats, usually I ignoring this, but this is for production purpose, so I'cant. Here's the answer and the caveat:

1

s = df2.set_index('Nation')['Capital City']
df1['Capital'] = df1['Country'].map(s).fillna(df1['Capital'])

caveat

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

2

df1['Capital'] = df1['Country'].replace(s)

caveat

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

3

s = df2.set_index('Nation')['Capital City']
df1['Capital'].update(df1['Country'].map(s))

caveat

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  exec(code_obj, self.user_global_ns, self.user_ns)
/home/ubuntu/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: 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

How to reduce the caveat

Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70

1 Answers1

1

Problem is obviously in one line before in filtering.

Solution is add copy like:

df1 = df[df['col'] == 10].copy()

Explanation:

If you modify values in df1 later you will find that the modifications do not propagate back to the original data (df), and that Pandas does warning.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252