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

如何移除DataFrame中的NaN与浮点格式,保留索引与原值

解决方法

代码实现

import pandas as pd

lst = [[2,25,26],[5],[10,19,19],[20,18,18]]
df1 = pd.DataFrame(lst)

# 转换数值为整数,将NaN替换为空字符串
df_result = df1.applymap(lambda x: int(x) if pd.notna(x) else '')
print(df_result)

输出结果

0    1    2
0   2   25   26
1   5          
2  10   19   19
3  20   18   18

说明

  • 用applymap遍历DataFrame每个元素:非NaN的数值转成整数,NaN直接替换为空字符串
  • 完整保留原数据的索引和有效数值,同时消除了小数格式和NaN占位符

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 23:15:02