我可以在pandas数据帧中为替换函数找到替代`.loc`替代方法

时间:2018-05-25 07:16:09

标签: python pandas dataframe

以前,我问How to update series based on other pandas dataframe,并且有3个答案,但其中3个给出了警告,通常我忽略了这一点,但这是出于生产目的,所以我不能。以下是答案和警告:

1

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

警告

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)

警告

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))

警告

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

如何减少警告

1 个答案:

答案 0 :(得分:1)

在过滤之前,问题显然在一行​​中。

Solution添加copy,如:

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

Explanation

如果稍后修改df1中的值,您会发现修改不会传播回原始数据(df),而Pandas会发出警告。