将 Pandas 数据帧中的列从 float 转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46438905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:32:00  来源:igfitidea点击:

Convert a column from pandas dataframe from float to int

pythonpandas

提问by niukasu

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. Try using .loc[row_indexer,col_indexer] = value instead

/home/ubuntu/src/anaconda3/lib/python3.5/site-packages/ipykernel/ main.py:2: SettingWithCopyWarning: 试图在 DataFrame 切片的副本上设置一个值。尝试使用 .loc[row_indexer,col_indexer] = value 代替

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copyfrom ipykernel import kernelapp as app

请参阅文档中的警告:http: //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy from ipykernel import kernelapp as app

采纳答案by sudonym

You need to disable chained assignments.

您需要禁用链式分配。

# Disable chained assignments
pd.options.mode.chained_assignment = None 

Will make it work.

会让它工作。