python-3.x - 将带小数的浮点数转换为不带小数点的字符串 Python 3.7
问题描述
努力将浮点数 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)
解决方案
你可以像这样简单地做到这一点:
df['variable'] = df['variable'].astype(int, errors = 'ignore').astype(str)
推荐阅读
- azure-data-factory - 基于参数的 Azure Datafactory 触发器
- admob - Admob:广告投放已停用,但 PIN 码和 PIN 卡均不可用
- r - 如何按组汇总百分比集群分配?
- python - 如何使用在 B 列的正则表达式模式上匹配 A 列中的字符串的结果在 Pandas 中构建一个新列?
- pandas - 从 Access 数据库中检索和打印值
- powershell - 在打开多个 PowerShell 窗口的脚本中调用 ifconfig
- ruby - 是否可以知道在步骤定义中使用了哪个 Gherkin 关键字?(红宝石)
- microsoft-teams - 微软团队的新 webhook 出现问题
- c# - C# Webclient.DownloadData 只返回垃圾
- python - 无法使用终端安装 python swagger 客户端