在Python中连接浮点数和字符串时遇到问题。

huangapple go评论49阅读模式
英文:

Trouble concatenating floats and strings in Python

问题

I'm using this code:

for each in x:
    descrVar = descrVar + " " + str(df.iloc[counter, each])

to iterate through a table and concatenate cells into a variable. The problem is, some of the cells will be a NaN. As a result, I'm getting the following error:

TypeError: can only concatenate str (not "numpy.float64") to str

I assume this means a NaN is a float64 and not a str. Is there any way around this, such as forcing every cell to convert to a str?

英文:

I'm using this code:

for each in x: 
            descrVar = descrVar + " " + df.iloc[counter,each]

to iterate through a table and concatenate cells into a variable. The problem is, some of the cells will be a Nan. As a result, I'm getting the following error:

TypeError: can only concatenate str (not "numpy.float64") to str

I assume this means a Nan is a float64 and not a str. Is there any way around this, such as forcing every cell to convert to a str?

答案1

得分: 1

An f-string will format your data as str.

for each in x:
    descrVar += f" {df.iloc[counter,each]}"
英文:

An f-string will format your data as str.

for each in x:
    descrVar += f" {df.iloc[counter,each]}"

huangapple
  • 本文由 发表于 2023年2月6日 08:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定