英文:
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]}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论