我有一个简单的过程,尝试将数组prob_win
添加为现有数据框df
的新列。它们具有如下所示的相同尺寸:
print type(prob_win)
print len(prob_win)
print df.shape
<type 'numpy.ndarray'>
799
(799, 1)
然后我做了以下任务:
df['prob_win'] = prob_win
代码有效,但有以下警告:
1 Warning
/opt/conda/envs/python2/lib/python2.7/site-packages/ipykernel/__main__.py:14: 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/indexing.html#indexing-view-versus-copy
所以我改为使用.loc作为警告说:
df.loc[:,'prob_win'] = prob_win
但仍然有同样的错误。我在这做错了什么?在这种情况下如何摆脱警告?谢谢!
答案 0 :(得分:2)
如果警告很重要。
df=df.assign(prob_win=prob_win)