Pandas dataframe.iloc [] with callable generates ValueError

Question:

I was trying to slice the data frame using the lambda function like this:

print(df.iloc[lambda x: (M < x.index < M + 5) | (x.index % N == 0), :])

M and N are variables, integers.

The error descripion:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Using just one comparison works without any problems

How can I fix this?

Asked By: Anton Grant

||

Answers:

Pandas Index does not support the a < x.index < b form:

df.loc[((M < df.index) & (df.index < M+5)) | (df.index % N == 0)]
Answered By: Corralien
M=5
N=2
print(df.iloc[[i for i in df.index if ((M < i < M + 5) or (i % N == 0))],:])
Answered By: Anushanga Perera
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.