gpt4 book ai didi

python - 将系列元素转换为 int

转载 作者:行者123 更新时间:2023-12-03 08:42:07 25 4
gpt4 key购买 nike

我有一个 Pandas 系列-

3959    2019
Name: DATE, dtype: int64

只有一个元素。

我想将该元素转换为整数。我做到了-

for i in last_row_year.to_numpy():
last_year=i

last_year获取为2019(int类型)

有没有更Pythonic的方法来做到这一点?

最佳答案

单例系列可以通过 .item() 转换为标量:

s = pd.Series([1]) 
s

0 1
dtype: int64

s.item()
# 1

请注意,这仅适用于长度为 1 的系列。对于更通用的解决方案,无论长度如何,始终获取任何系列的第一个元素,.iloc[0].to_numpy ()[0] 是首选。

关于python - 将系列元素转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62435533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com