You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何从Int64Index类型结果中提取整数值存入列表

Python提取Int64Index中有效整数的实现方法

你当前得到的列表元素是pandas的Int64Index类型对象,只需过滤掉空索引,再提取内部存储的数值即可得到目标整数列表。

方法1:修改原有循环,直接生成符合要求的列表

在遍历匹配日期时先判断匹配结果是否为空,非空时再提取数值存入列表,从根源避免后续二次处理:

index_earning_value = []
print(actual_date_name)
for y in actual_date_name:
    match_idx = df_stock.index[df_stock['timestamp']==str(y)]
    # 非空判断,避免空索引取值报错
    if len(match_idx) > 0:
        # 提取Int64Index中存储的整数值加入列表
        index_earning_value.append(match_idx[0])

print(index_earning_value)

运行后直接得到[88, 151, 212, 276]的目标结果。

方法2:从已生成的Int64Index列表中提取

如果不想修改原有逻辑,已经拿到了包含Int64Index的原始列表,可以直接遍历过滤取值:

# 你当前得到的原始输出列表
raw_list = [Int64Index([88], dtype='int64'), Int64Index([151], dtype='int64'), Int64Index([212], dtype='int64'), Int64Index([276], dtype='int64'), Int64Index([], dtype='int64')]
res_list = []
for idx_obj in raw_list:
    if len(idx_obj) > 0:
        res_list.append(idx_obj[0])

也可以用列表推导式简写实现:

res_list = [idx_obj[0] for idx_obj in raw_list if len(idx_obj) > 0]

内容的提问来源于stack exchange,提问作者aryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 21:45:07