gpt4 book ai didi

python - pandas 中的SettingWithCopyWarning : how to set the first value in a column?

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:37 25 4
gpt4 key购买 nike

运行我的代码时,我收到以下消息:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
df['detect'][df.index[0]] = df['event'][df.index[0]]

将一列的第一个值设置为等于另一列的第一个值的正确方法是什么?

最佳答案

使用ix执行索引标签选择:

In [102]:

df = pd.DataFrame({'detect':np.random.randn(5), 'event':np.arange(5)})
df
Out[102]:
detect event
0 -0.815105 0
1 -0.656923 1
2 -1.417722 2
3 0.210070 3
4 0.211728 4
In [103]:

df.ix[0,'detect'] = df.ix[0,'event']
df
Out[103]:
detect event
0 0.000000 0
1 -0.656923 1
2 -1.417722 2
3 0.210070 3
4 0.211728 4

你正在做的事情叫做chained indexing并且可能会或可能不会工作,因此会出现警告

关于python - pandas 中的SettingWithCopyWarning : how to set the first value in a column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28087429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com