2 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
applyround連續(xù)調(diào)用每一列上的函數(shù)。DataFrame 列是Series對(duì)象,這些__round__列上定義了一個(gè)dunder 方法,其行為略有不同。這實(shí)際上是round調(diào)用Series.
round(a[0])
0 1.0
1 1.0
Name: 0, dtype: float64
# Same as,
a[0].__round__()
0 1.0
1 1.0
Name: 0, dtype: float64
將此與 Pythonround在標(biāo)量上的典型行為進(jìn)行對(duì)比:
round(1.5)
# 2
# Same as,
(1.5).__round__()
# 2
如果您想要相同的行為,請(qǐng)使用applymap.
a.applymap(round)
0 1
0 1 3
1 1 5
這適用round于每個(gè)元素(標(biāo)量),向下取整為整數(shù)。
或者,我推薦的解決方案,
a.round().astype(int)
0 1
0 1 3
1 1 5
請(qǐng)注意,這不會(huì)對(duì)包含缺失數(shù)據(jù) (NaN) 的列進(jìn)行類型轉(zhuǎn)換。

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
a = a.apply(round).astype(dtype=np.int64)
只需使用它astype
即可將您float
的integer
.
添加回答
舉報(bào)