繁体   English   中英

如何在 Python 的 Pandas 数据框中使用 NaN 值更改列中浮点值的格式?

[英]How to change format of floar values in column with also NaN values in Pandas Data Frame in Python?

我在 Python 中有 Pandas DataFrame,如下所示:

col
-------
7.0
2.0
NaN
...

“col1”是浮点数据类型,但我想将此列中浮点值的显示从例如 7.0 转换为 7。我不能简单地将日期类型更改为 int,因为我在 col1 中也有“NaN”值。

因此,我需要以下内容:

col
-------
7
2
NaN
...

我怎样才能在 Python Pandas 中做到这一点?

您可以使用convert_dtypes执行自动转换:

df = df.convert_dtypes('col')

对于所有列:

df = df.convert_dtypes()

输出:

    col
0     7
1     2
2  <NA>

转换后:

df.dtypes

col    Int64
dtype: object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM