【发布时间】:2016-12-22 22:34:48
【问题描述】:
给定一个由多列组成的数据框df:
Col1 Col2 Col3 Col4 Col5 Col6
4 2 5 3 4 1
8 3 9 7 4 5
1 3 6 7 4 7
我想为一组列应用函数func
df.apply(lambda x: func(x[['Col1', 'Col2', 'Col3']]), axis=1)
这可以正常工作。但是,使用
df.apply(lambda x: func(x.iloc[:,0:3]), axis=1)
我收到以下错误:
IndexingError: ('Too many indexers', u'occurred at index 0')
由于我想使用以三列为一组的循环来自动化函数,我更喜欢使用 pandas iloc 或 ix 作为索引方法。
有人能解释一下这个错误吗?
【问题讨论】:
标签: python pandas indexing dataframe multiple-columns