我可以用另一种方式解决这个问题;但是,我有兴趣确切了解为什么尝试使用列表理解来迭代 pandas DataFrame 不起作用。(这a
是一个数据框)
def func(a,seed1,seed2):
for i in range(0,3):
# Sum of squares. Results in a series containing 'date' and 'num'
sorted1 = ((a-seed1)**2).sum(1)
sorted2 = ((a-seed2)**2).sum(1)
# This makes a list out of the dataframe.
a = [a.ix[i] for i in a.index if sorted1[i]<sorted2[i]]
b = [a.ix[i] for i in a.index if sorted1[i]>=sorted2[i]]
# The above line throws the exception:
# TypeError: 'builtin_function_or_method' object is not iterable
# Throw it back into a dataframe...
a = pd.DataFrame(a,columns=['A','B','C'])
b = pd.DataFrame(b,columns=['A','B','C'])
# Update the seed.
seed1 = a.mean()
seed2 = b.mean()
print a.head()
print "I'm computing."