简体   繁体   English

如何修复 Python 中的 TypeError

[英]How do I fix TypeError in Python

It shows the following error它显示以下错误

TypeError: can only concatenate str (not "int") to str

Here is the code这是代码

data.loc[(data['Account'] == Value) & (data['Contract End Month'] >= 7),
             'End Fiscal Year'] = data['Contract End Year'] + 1

Looks like your data['Contract End Year'] is an str instance, try casting it into an int before performing the addition:看起来你的data['Contract End Year']是一个str实例,在执行添加之前尝试将其转换为int

data.loc[(data['Account'] == Value) & (data['Contract End Month'] >= 7),'End Fiscal Year'] = int(data['Contract End Year']) + 1

The error message is indicating that you are trying to concatenate a string with an integer. It seems like the column 'Contract End Year' is of type string, but you're trying to add 1 to it as if it were an integer. You can convert it to int using pandas.to_numeric method and then adding 1, and later you can convert it back to str错误消息表明您正在尝试将字符串与 integer 连接起来。看起来“Contract End Year”列的类型为字符串,但您正在尝试向其添加 1,就好像它是 integer 一样。您可以使用 pandas.to_numeric 方法将其转换为 int,然后加 1,稍后您可以将其转换回 str

data['Contract End Year'] = pd.to_numeric(data['Contract End Year'], errors='coerce')
data.loc[(data['Account'] == Value) & (data['Contract End Month'] >= 7),
             'End Fiscal Year'] = data['Contract End Year'] + 1
data['End Fiscal Year'] = data['End Fiscal Year'].astype(str)

This should fix the TypeError and allow you to perform the arithmetic operation on the 'Contract End Year' column.这应该修复 TypeError 并允许您对“合同结束年份”列执行算术运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 Python 中的“TypeError”? - How do I fix a “TypeError” in Python? 如何在python中修复TypeError - How to fix a TypeError in python 如何在python 3.3中修复此错误? TypeError:不可排序的类型:str()&lt;int() - how do I fix this error in python 3.3? TypeError: unorderable types: str() < int() 如何在 Python 中修复关于非整数和浮点数的这个 TypeError? - How do I fix this TypeError in Python concerning non-int and floats? 如何修复我的 python Yahoo Finance Webscrapper 中的类型错误、解析和所有其他错误 - How do I fix TypeError, Parsing, and all other errors in my python Yahoo Finance Webscrapper 如何修复此错误:TypeError: unsupported operand type(s) for *: 'float' and 'function' in python - How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'function' in python 你如何用 python 修复 - TypeError: must be str, nor int -? - How do you fix with python - TypeError: must be str, nor int -? 我收到一个类型错误。 我如何解决它? - I'm getting a TypeError. How do I fix it? 我有一个 python TypeError,我不知道如何修复它 - I have a python TypeError and I don't know how to fix it 如何修复“ TypeError:&#39;模块&#39;对象不可调用”? - How do i fix “TypeError: 'module' object is not callable”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM