简体   繁体   English

如何在我的 pandas apply 语句中避免此索引错误?

[英]How can I avoid this indices error in my pandas apply statement?

I'm really lost on this error message, particularly as it seems to only occur in my 64 bit version of pandas, and not a slightly older 32 bit version I have in a different environment.我真的迷失了这个错误消息,特别是因为它似乎只出现在我的 64 位版本的 pandas 中,而不是我在不同环境中使用的稍旧的 32 位版本。

So I have two dataframes- "summary" and "all".所以我有两个数据框-“摘要”和“全部”。 "Summary" includes start times of types of events, whilst "all" contains data that's being interpreted into the summary table. “摘要”包括事件类型的开始时间,而“全部”包含正在解释到摘要表中的数据。

This line below that takes a 'starting reading' seems to be causing all the issues, not sure why though!下面这行需要“开始阅读”似乎导致了所有问题,但不知道为什么!

summary['average_after_start'] = summary.apply(lambda row: np.mean(all[(all['Timestamp'] >= row['Start'])]['Value']), axis=1, raw=True)

The error message I get is this:我得到的错误信息是这样的:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Any pointers would be appreciated!任何指针将不胜感激!

*edit: I've simplified this a bit to be the simplest code I can find so far that causes the issue *编辑:我已将其简化为迄今为止我能找到的最简单的代码,这会导致问题

If you set raw=True then row is a numpy array, not a pandas series and hence row['Start'] will throw an error as you can't index an array with a string.如果设置raw=Truerow是 numpy 数组,而不是 pandas 系列,因此row['Start']将引发错误,因为您无法使用字符串索引数组。
So you should change it to raw=False (which is the default, so you can leave it out).因此,您应该将其更改为raw=False (这是默认设置,因此您可以将其省略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM