【发布时间】:2021-08-06 14:09:30
【问题描述】:
努力将浮点数 12345678.0 转换为字符串 12345678 经过我在 SO 中找到的几种不同的解决方案,但似乎无法到达终点,小数点不会下降。
这是我尝试过的
df["variable"]= df["variable"].astype(str)
df["variable"]= df["variable"].astype(int).astype(str)
# old lambda solution
cols = ['variable']
for col in cols:
df[col] = df[col].apply(lambda x: int(x) if x == x else "")
# convert int to string
df["variable"] = df["variable"].astype(str)
【问题讨论】:
-
您可以使用
.astype('Int64'),如果您无法将您的系列安全地转换为int,则会引发错误,同时还允许您在该列中存储NaN
标签: python-3.x pandas dataformat