任何人都可以帮我理解我哪里出了问题吗?
import os
import numpy as np
import pandas as pd
ctr_x = []
tl_list = []
df = pd.read_csv('ring_1_05_sam.csv')
ctr_x = df.iloc[8:12, 0:1]
ctr_y = df.iloc[8:12, 1:2]
ctr_x = (ctr_x.to_string(index=False, header=False))
ctr_y = (ctr_y.to_string(index=False, header=False))
tl_list.append((str(ctr_x - 30),str(ctr_y - 30)))
print(tl_list)
我收到错误:
Traceback (most recent call last):
File "annotation.py", line 9, in <module>
tl_list.append((str(ctr_x - 30),str(ctr_y - 30)))
TypeError: unsupported operand type(s) for -: 'str' and 'int'
正如评论中提到的:
我尝试将每个值减去 30 并存储在 tl_list[]
中。
最佳答案
I am trying to subtract each value by 30 and store in tl_list[].
字符串转换会分散注意力并且没有必要。您也不需要显式实例化并附加到列表。使用pd.Series.tolist
相反:
res_x = (df.iloc[8:12, 0] - 30).tolist()
res_y = (df.iloc[8:12, 1] - 30).tolist()
res = [res_x, res_y]
或者结合您的操作并使用np.ndarray.tolist
:
res = (df.iloc[8:12, 0:2] - 30).values.tolist()
关于python - 不支持的操作数类型 - : 'str' and 'int' in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53524973/