0
import yfinance as yf
import pandas as pd
import datetime
import pandas_datareader.data as web

# start and end dates
from pandas import Int64Index

start = datetime.date(2020, 9, 17)
end = datetime.date(2021, 9, 16)

# output dataframe from YahooFinance
dFrame = web.DataReader('ADA-USD', 'yahoo', start, end)
pd.options.display.width = 0
pd.set_option("display.max_rows", 2000)
print(dFrame)

I need to be able to get the Close prices for Ada-Cardano between $0.10 and $1.00. How do I do that? I tried a for loop with and if statement but didn't seem to work.

J.R. BEATS
  • 93
  • 6

2 Answers2

0

I'm not really sure that I correctly understand your question. But if this is not what you are looking for I will be happy to change the answer after clarification.

If what you want is to get values between 0.1 and 1 in a list. You can do this,

ls = [your closing values] # list with closing values
new_ls = [] # new list with closing values between 0.1 and 1

for i in ls:
    if i < 1 and i > 0.1:
        new_ls.append(i)

Or a more efficient method,

ls = [your closing values]
new_ls = [i for i in ls if i < 1 and i > 0.1]
0

You could use something like this.

# create filter for Close>= 0.10 and Close<=1.00
mask = (dFrame['Close']>=0.10) & (dFrame['Close']<=1.00)

# filter data
dFrame_filtered = dFrame[mask]

print(dFrame_filtered.head())
High Low Open Close Volume Adj Close
Date
2020-09-26 0.102269 0.094908 0.095578 0.101206 495061617.0 0.101206
2020-09-27 0.106491 0.100503 0.101206 0.100696 573226386.0 0.100696
2020-09-28 0.103349 0.097939 0.100703 0.101136 450401208.0 0.101136
2020-09-29 0.101345 0.097262 0.101116 0.101146 396298082.0 0.101146
2020-10-09 0.110540 0.101445 0.101466 0.105025 923715999.0 0.105025
norie
  • 9,609
  • 2
  • 11
  • 18
  • 1
    Check out [`Series.between`](https://pandas.pydata.org/docs/reference/api/pandas.Series.between.html) which is specifically designed for this use-case `dFrame[dFrame['Close'].between(0.1, 1)`. As an aside, I'd avoid shadowing the builtin `filter` function with a boolean index the conventional name in pandas is `m` for mask. – Henry Ecker Sep 17 '21 at 03:49