TypeError: cannot concatenate object of type '<class 'int'>'; only Series and DataFrame objs are valid
时间: 2024-04-29 17:21:18 浏览: 285
解决Python 异常TypeError: cannot concatenate str and int objects
3星 · 编辑精心推荐
This error occurs when you try to concatenate an object of type 'int' with another object using the '+' operator. However, the '+' operator is only defined for concatenating Series and DataFrame objects in pandas.
For example, if you have two Series objects s1 and s2, you can concatenate them using the '+' operator like this:
```
s3 = s1 + s2
```
But if you try to concatenate an integer with a Series object, you will get the TypeError:
```
s3 = s1 + 5 # raises TypeError
```
To fix this error, make sure that you are only concatenating Series and DataFrame objects using the '+' operator. If you need to concatenate an integer with a Series object, you can convert the integer to a Series object first:
```
s3 = s1 + pd.Series([5])
```
阅读全文