python-3.x - 我不知道为什么我在这里得到“只能将 str(而不是“int”)连接到 str”错误
问题描述
代码:
import pandas as pd
app = pd.read_excel("/Users/michaelblack/Downloads/Applicants with Interest/Applicants with Interests 2020.xlsx")
app = app.replace(',',' ', regex=True).replace('/','', regex=True).replace('-','',regex=True).replace(';',' ',regex=True)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
app.fillna(' ', inplace = True)
app.info()
ct = 0
while ct <= 9:
if ct == 0:
app['new'+str(ct)] = app['Interest Description'] + ' ' + app['Interest Role']
else:
app['new'+str(ct)] = app['Interest Description' + '.' + str(ct)] + ' '+ app['Interest Role' + '.' + str(ct)]
ct += 1
app.head(5)
我的错误在第 12 行(ct += 1 正上方),它是“只能将 str(而不是“int”)连接到 str”。
对于上下文:我的 Excel 文件中的每个字段都是一个“对象”数据类型,我还有另外两个 Excel 文件,它们的列数、列标题和数据类型完全相同,但值不同,它们都可以工作。
有人可以在这里提供一些指导吗?为了我的一生,我迷失了。当代码完全相同时,我无法理解为什么一个相同的 Excel 文件可以工作,而另一个却不能。
解决方案
尝试将 .astype(str) 添加到每一列:
while ct <= 9:
if ct == 0:
app['new'+str(ct)] = app['Interest Description'].astype(str) +' '+ app['Interest Role'].astype(str)
else:
app['new'+str(ct)] = app['Interest Description' + '.' + str(ct)].astype(str) + ' '+ app['Interest Role' + '.' + str(ct)].astype(str)
ct += 1
推荐阅读
- count - Qlik Sense - 具有多个逻辑/过滤器实施的 COUNT Measure
- go - 为什么这些 Go 类型不相同?
- angular - 离子侧边菜单 - view.ts 中的触发功能
- android - 如何设置 MutableLiveData 的值
() 在 Kotlin 中是不利的吗? - c++ - 使用 ffmpeg API 将 pcm_alaw 类型的音频数据包装成 MKA 音频文件
- excel - 做直到什么都没找到
- json - angular.json 资产:使用通配符作为名称
- c - 如何shell命令注入文件
- node.js - 在某些情况下,将 nodejs 缓冲区转换为字符串并返回到缓冲区会产生不同的结果
- typescript - 在 Typescript 中指定具有至少一个已知属性的对象