【发布时间】:2022-01-23 19:20:37
【问题描述】:
我是 pandas 的初学者,正在为 for 循环而苦苦挣扎。
我将以下 excel 文件加载到 pandas:
x=pd.read_excel('BTC.xlsx',)
x
这是我得到的:
BTC obtained
0 0.00567
1 0.00054
2 0.00230
我想使用 for 循环来迭代数字:
n=0
for v in x.iteritems():
n+=v
print(n)
当我运行它时,我总是收到以下错误:
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
有人可以帮我解决这个问题吗?
【问题讨论】:
-
我猜你在 for 循环中遇到了错误。 iteritems() 接受两个参数,而不仅仅是单个 v。请参阅pandas.pydata.org/docs/reference/api/… 的文档
-
是的,你是对的。但我想得到以下结果:0.00567、0.00621 (0.00567+0.00054) 和 0.00851 (0.00621+0.00230)。
标签: python pandas dataframe for-loop iteration