[英]Convert a column from pandas dataframe from float to int
When I want to do the following command(learned from the other question), there is a warning.当我想执行以下命令(从另一个问题中学到)时,会出现警告。 How to avoid this warning?
如何避免这个警告?
df['Class'] = df['Class'].astype(int)
/home/ubuntu/src/anaconda3/lib/python3.5/site-packages/ipykernel/ main .py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. /home/ubuntu/src/anaconda3/lib/python3.5/site-packages/ipykernel/ main .py:2: SettingWithCopyWarning: 试图在 DataFrame 的切片副本上设置值。 Try using .loc[row_indexer,col_indexer] = value instead
尝试使用 .loc[row_indexer,col_indexer] = value 代替
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy from ipykernel import kernelapp as app请参阅文档中的警告: http ://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy from ipykernel import kernelapp as app
You need to disable chained assignments.您需要禁用链式分配。
# Disable chained assignments
pd.options.mode.chained_assignment = None
Will make it work.会让它工作。
I recommend you this way rather than changing configurations though this would take more resources.我建议您采用这种方式而不是更改配置,尽管这会占用更多资源。 With abundant memory or small size data, it doesn't matter.
有充足的内存或小尺寸的数据,都没有关系。
df_ = df.copy()
df_['Class'] = df_['Class'].astype(int)
FYI仅供参考
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-view-versus-copy https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing -查看与复制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.