【发布时间】:2019-12-22 04:26:36
【问题描述】:
我正在尝试将预测列从df_B 附加到df_A。
df_A['prediction'] = df_B['prediction']
但该列未正确复制,我收到以下错误:
/Users/edamame/workspace/git/tensorplay/venv/lib/python3.7/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
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
"""Entry point for launching an IPython kernel.
有没有办法在不循环数据框的情况下正确执行列复制?
【问题讨论】:
-
您的方法正确。但是
df_A来自另一个数据框,即您可能在某个时候做了df_A = some_other_df[some_conditions]。现在,每当您更改df_A时,pandas 都会警告您说您正在更改 some_other_df 的 copy,而不是some_other_df本身.要解决这个问题,您可以使用df_A = some_other_df[some_conditions].copy()使其独立于之前的df -
我在这里回答了这个问题:stackoverflow.com/questions/57494760/…
-
我喜欢的另一个修复是
df_A = some_other_df[condition][:]虽然它不是很健壮
标签: python python-3.x pandas