python - 不支持的操作数类型 - : 'str' and 'int' in python

标签 python python-3.x pandas

任何人都可以帮我理解我哪里出了问题吗?

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/

相关文章:

python - 如何在 dataframe.set_value 中使用 "takeable=True"和索引器,尤其是多重索引

python - 如何在 Gedit 插件中获取当前行的长度(或行尾的偏移量)

python - 如何打乱列表,以便每个特定长度的子列表都有唯一的项目?

python-3.x - Python 3 inside Sublime Text with Anaconda on a mac

python-3.x - 有没有办法将我的 python 应用程序与其所有依赖项捆绑在一起,以便我可以将它分发给普通用户?

python - 执行操作时恢复列

python - 无法让 mysql 与 django 一起工作

python - 如何正确使用 PyQuery 遍历?

python - 如何在 Python 中拆分和解析字符串?

python - Pandas 保存时添加一个空列