我已经尝试了30分钟来调试我的代码,但一直没有成功,也许你可以帮忙吗?
错误出现在第17行,但我真的不知道为什么会出错。
所以,我使用一个字典来存储我的函数is_nontrivial_reverse_number()返回的数据,它是一个布尔类型。我遍历整个字典,并插入所有从0到1100的值。
然后,我将我的字典插入到pandas库的DataFrame中。
接着,我遍历我的DataFrame以检索设置为true的值的数量。
但问题是,row["nonTrivial"]似乎不起作用,即使我在另一个字典和DataFrame集上使用了相同的函数。
错误出现在第17行,但我真的不知道为什么会出错。
所以,我使用一个字典来存储我的函数is_nontrivial_reverse_number()返回的数据,它是一个布尔类型。我遍历整个字典,并插入所有从0到1100的值。
然后,我将我的字典插入到pandas库的DataFrame中。
接着,我遍历我的DataFrame以检索设置为true的值的数量。
但问题是,row["nonTrivial"]似乎不起作用,即使我在另一个字典和DataFrame集上使用了相同的函数。
import pandas as pd2
dic2 = {'nonTrivial': [is_nontrivial_reverse_number(x) for x in range(0, 1100)]}
dic2
df2 = pd2.DataFrame(dic2)
df2 = df2.set_index('nonTrivial')
#df2.head()
##df2.shape
#df.dtypes
for row in df2.iterrows():
if (row["nonTrivial"]==True):
n = n+1
print (n)
预期结果:n=2
实际结果:
TypeError Traceback (most recent call last)
<ipython-input-314-ce7c400929e6> in <module>
15
16 for row in df2.iterrows():
---> 17 if (row["nonTrivial"]==True):
18 n = n+1
19 print (n)
TypeError: tuple indices must be integers or slices, not str
for row in df2.iterrows()
更改为for idx, row in df2.iterrows()
。 - ayhan