python-3.x - 我想如何将 int 转换为 str
问题描述
我想将重要列的值组合成一个字符串,但其中一列是整数。而且真的不知道在哪里放置 str() 代码以将 int 转换为字符串。所以这是我的代码:
#create function to combine the values of the important columns into a single string
def get_important_features(data):
important_features = []
for i in range(0, data.shape[0]):
important_features.append(data['Location'][i]+' '+str(data['Price'])[i]+' '+data['Property Type'][i]+' '+data['Furnishing'][i])
return important_features
**#create a column to hold the combined strings**
df['important_features'] = get_important_features(df)
#show the data
df.head(3)
这是错误输出:
TypeError Traceback (most recent call last)
<ipython-input-16-ef130dbef96b> in <module>()
1 #create a column to hold the combined strings
----> 2 df['important_features'] = get_important_features(df)
3
4 #show the data
5
<ipython-input-15-fda6e7b86a60> in get_important_features(data)
3 important_features = []
4 for i in range(0, data.shape[0]):
----> 5 important_features.append(data['Location'][i]+' '+str(data['Price'])[i]+' '+data['Property Type'][i]+' '+data['Furnishing'][i])
6
7 return important_features
TypeError: must be str, not float
解决方案
谢谢你们。我已经解决了答案。谢谢大家的想法
我编辑这一行:
important_features.append(data['Location'][i]+' '+str(data['Price'][i])+' '+data['Property Type'][i]+' '+data['Furnishing'][i])
并像这样改变:
important_features.append(str(data['Location'][i])+' '+str(data['Price'][i])+' '+str(data['Property Type'][i])+' '+str(data['Furnishing'][i]) )
推荐阅读
- python - 有没有一种方法可以从以下 list_A 创建一个子列表,在 python 中输出如下
- python - 散景添加动作工具以隐藏图例
- mysql - Ruby on Rails 5.2.0 Mysql2::Error::ConnectionError SSL 连接错误:未知错误号
- python-3.x - 如何在sqlAlchemy中创建接口表(关系多对多)
- php - 通过 .htaccess 设置日志目的地
- javascript - 在自定义表中搜索 - React
- machine-learning - PyTorch Detecto 模型:张量在预训练模型的预测中不兼容
- c++ - 为存储桶保留多少空间 std::unordered_map 和 boost::unordered_map 以及为什么?
- android - 导出的应用程序(android)没有全部功能
- c# - 图片不会上传到 REST api