pandas 将 float 系列中的所有元素转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33158749/
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
Convert all elements in float Series to integer
提问by Data Enthusiast
I have a column, having float values,in a dataframe (so I am calling this column as Float series). I want to convert all the values to integer or just round it up so that there are no decimals.
我在数据框中有一列具有浮点值的列(因此我将此列称为浮点系列)。我想将所有值转换为整数或将其四舍五入以便没有小数。
Let us say the dataframe is df and the column is a, I tried this :
让我们说数据框是 df 列是 a,我试过这个:
df['a'] = round(df['a'])
I got an error saying this method can't be applied to a Series, only applicable to individual values.
我收到一条错误消息,说此方法不能应用于系列,只能应用于单个值。
Next I tried this :
接下来我尝试了这个:
for obj in df['a']:
obj =int(round(obj))
After this I printed df but there was no change. Where am I going wrong?
在此之后,我打印了 df 但没有任何变化。我哪里错了?
回答by EdChum
roundwon't work as it's being called on a pandas Series which is array-like rather than a scalar value, there is the built in method pd.Series.roundto operate on the whole Series array after which you can change the dtype using astype:
round将无法工作,因为它是在类似数组而不是标量值的 Pandas Series 上调用的,有内置方法pd.Series.round可以对整个 Series 数组进行操作,之后您可以使用astype以下方法更改 dtype :
In [43]:
df = pd.DataFrame({'a':np.random.randn(5)})
df['a'] = df['a'] * 100
df
Out[43]:
a
0 -4.489462
1 -133.556951
2 -136.397189
3 -106.993288
4 -89.820355
In [45]:
df['a'] = df['a'].round(0).astype(int)
df
Out[45]:
a
0 -4
1 -134
2 -136
3 -107
4 -90
Also it's unnecessary to iterate over the rows when there are vectorised methods available
当有可用的矢量化方法时,也没有必要对行进行迭代
Also this:
还有这个:
for obj in df['a']:
obj =int(round(obj))
Does not mutate the individual cell in the Series, it's operating on a copy of the value which is why the df is not mutated.
不改变系列中的单个单元格,它在值的副本上运行,这就是为什么 df 没有改变的原因。
回答by Chad S.
The code in your loop:
循环中的代码:
obj = int(round(obj))
obj = int(round(obj))
Only changes which object the name objrefers to. It does not modify the data stored in the series. If you want to do this you need to know where in the series the data is stored and update it there.
仅更改名称obj所指的对象。它不会修改存储在系列中的数据。如果您想这样做,您需要知道数据在系列中的存储位置并在那里更新。
E.g.
例如
for i, num in enumerate(df['a']):
df['a'].iloc[i] = int(round(obj))
回答by Arthur D. Howland
When converting a float to an integer, I found out using df.dtypes that the column I was trying to round off was an object not a float. The round command won't work on objects so to do the conversion I did:
将浮点数转换为整数时,我发现使用 df.dtypes 我试图四舍五入的列是一个对象而不是浮点数。round 命令不适用于对象,因此要进行我所做的转换:
df['a'] = pd.to_numeric(df['a'])
df['a'] = df['a'].round(0).astype(int)
df['a'] = pd.to_numeric(df['a'])
df['a'] = df['a'].round(0).astype(int)
or as one line:
或作为一行:
df['a'] = pd.to_numeric(df['a']).round(0).astype(int)
回答by fantabolous
If you specifically want to round upas your question states, you can use np.ceil:
如果您特别想按照您的问题进行汇总,您可以使用np.ceil:
import numpy as np
df['a'] = np.ceil(df['a'])
See also Floor or ceiling of a pandas series in python?
Not sure there's much advantage to type converting to int; pandas and numpy love floats.
不确定将类型转换为 int 有多大优势;Pandas和麻木的爱漂浮。

