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?
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)]
M=5
N=2
print(df.iloc[[i for i in df.index if ((M < i < M + 5) or (i % N == 0))],:])
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?
Pandas Index does not support the a < x.index < b
form:
df.loc[((M < df.index) & (df.index < M+5)) | (df.index % N == 0)]
M=5
N=2
print(df.iloc[[i for i in df.index if ((M < i < M + 5) or (i % N == 0))],:])