TypeError: can only concatenate str (not “int“) to str
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: can only concatenate str (not “int“) to str相关的知识,希望对你有一定的参考价值。
TypeError: can only concatenate str (not "int") to str
目录
TypeError: can only concatenate str (not "int") to str
问题:
#gp字段原本为int型;cheid字段为str型;
#转为object直接相加是有问题的;
#不转换直接相加也是有问题的;
df_dict.info()
# df = df.loc[0:206000].astype('float32',errors = 'ignore')
#df_dict['GP'] = df_dict['GP'].astype('object')
#or
df_dict['ID'] = df_dict['CHEID'] + df_dict['GP']
解决:
.astype('str')
df_dict.info()
# df = df.loc[0:206000].astype('float32',errors = 'ignore')
df_dict['GP'] = df_dict['GP'].astype('str')
df_dict['ID'] = df_dict['CHEID'] + df_dict['GP']
#其他示例
#如果字段都是str型
df["period"] = df["Year"] + df["quarter"]
#如果不是str型,那么转换后再相加也不迟;
df["period"] = df["Year"].astype(str) + df["quarter"]
完整错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\\anaconda\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py in _na_arithmetic_op(left, right, op, is_cmp)
141 try:
--> 142 result = expressions.evaluate(op, left, right)
143 except TypeError:
D:\\anaconda\\lib\\site-packages\\pandas\\core\\computation\\expressions.py in evaluate(op, a, b, use_numexpr)
234 # error: "None" not callable
--> 235 return _evaluate(op, op_str, a, b) # type: ignore[misc]
236 return _evaluate_standard(op, op_str, a, b)
D:\\anaconda\\lib\\site-packages\\pandas\\core\\computation\\expressions.py in _evaluate_standard(op, op_str, a, b)
68 with np.errstate(all="ignore"):
---> 69 return op(a, b)
70
TypeError: can only concatenate str (not "int") to str
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-208-94f0d1210a8f> in <module>
2 # df = df.loc[0:206000].astype('float32',errors = 'ignore')
3 df_dict['GP'] = df_dict['GP'].astype('object')
----> 4 df_dict['ID'] = df_dict['CHEID'] + df_dict['GP']
D:\\anaconda\\lib\\site-packages\\pandas\\core\\ops\\common.py in new_method(self, other)
63 other = item_from_zerodim(other)
64
---> 65 return method(self, other)
66
67 return new_method
D:\\anaconda\\lib\\site-packages\\pandas\\core\\arraylike.py in __add__(self, other)
87 @unpack_zerodim_and_defer("__add__")
88 def __add__(self, other):
---> 89 return self._arith_method(other, operator.add)
90
91 @unpack_zerodim_and_defer("__radd__")
D:\\anaconda\\lib\\site-packages\\pandas\\core\\series.py in _arith_method(self, other, op)
4996 lvalues = extract_array(self, extract_numpy=True)
4997 rvalues = extract_array(other, extract_numpy=True)
-> 4998 result = ops.arithmetic_op(lvalues, rvalues, op)
4999
5000 return self._construct_result(result, name=res_name)
D:\\anaconda\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py in arithmetic_op(left, right, op)
187 else:
188 with np.errstate(all="ignore"):
--> 189 res_values = _na_arithmetic_op(lvalues, rvalues, op)
190
191 return res_values
D:\\anaconda\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py in _na_arithmetic_op(left, right, op, is_cmp)
147 # will handle complex numbers incorrectly, see GH#32047
148 raise
--> 149 result = _masked_arith_op(left, right, op)
150
151 if is_cmp and (is_scalar(result) or result is NotImplemented):
D:\\anaconda\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py in _masked_arith_op(x, y, op)
89 if mask.any():
90 with np.errstate(all="ignore"):
---> 91 result[mask] = op(xrav[mask], yrav[mask])
92
93 else:
TypeError: can only concatenate str (not "int") to str
参考:Combine two columns of text in pandas dataframe
参考:python
以上是关于TypeError: can only concatenate str (not “int“) to str的主要内容,如果未能解决你的问题,请参考以下文章
TypeError: only integer scalar arrays can be converted to a scalar index
TypeError: only integer scalar arrays can be converted to a scalar index
请解决这个 TypeError: can only concatenate str (not "int") to str
解决问题:only integer scalar arrays can be converted to a scalar index