问题是,只需重复
grouped
本身,您迭代序列中的值,这些值只是
Amount
列。还要注意
ticker
,
action
,
date
和
time
是序列的索引,而不是其值。因此,您试图分配
ticker, action, date, time
一个浮球。因此错误
TypeError: 'float' object is not iterable
. 在python 3中,错误更有用,因为
TypeError: cannot unpack non-iterable float object
.
要修复此问题,应使用
iteritems
(
docs
)熊猫系列类的方法。这将迭代序列中的每个项,并在每次迭代中以元组的形式返回索引和值。因为您有一个复合索引,所以该索引也将是一个元组,您可以使用如下方式将其解包成不同的值:
for (ticker, action, date, time), amount in grouped.iteritems():
print(ticker)
print(action)
print(date)
print(time)
编辑:
[解决问题编辑。]
在您所提供的代码示例中,无论您如何调用,在某种意义上,这些代码标签都是唯一的。
some_function
在同一个断续器上可能会出现多次,因此断续器实际上不需要是唯一的。也许你能做的就是这样:
grouped = df_orders.groupby(['ticker', 'date', 'unit'])['amount'].agg(sum)
total = 0
for (ticker, date, unit), amount in grouped.iteritems():
if unit == 'SHARES':
total += share_function(ticker, date)
else:
total += other_function(ticker, date)