-1
import yfinance as yf
df = yf.Ticker('spy').history(period='100d', interval='1d')

if df[df['Open'] < 400] :
    print(df)

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


if (df['Open'] < 400).any():
    print(df)

any()All output,Those larger than 400 will also be printed。


if (df['Open'] < 400).all():
    print(df)

all()No error reported,No output at all。

How to solve this problem? Thank you!

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • Without example data how can we help? Please read [mre]. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii May 24 '22 at 18:25
  • What is the column's type? ... `df.dtypes` , `df['Open'].dtype` – wwii May 24 '22 at 18:27

1 Answers1

1

Rather than checking if the boolean Series is true or false, use it as an index.

print(df[df['Open' < 400]])
Zorgoth
  • 499
  • 3
  • 9