Selecting data from data range
Pulling data that meets specific conditions is one of the most powerful and commonly used operations with DataFrames. You can try it now with Alphabet stock data. Provided is the DataFrame alphabet
and the datetime
s start_date
and end_date
. The DataFrame's head looks like this:
date | close | volume | open | high | low |
---|---|---|---|---|---|
2019-08-02 | 1196.32 | 1745450 | 1203.00 | 1209.500 | 1190.00 |
2019-08-01 | 1211.78 | 1771271 | 1217.63 | 1236.298 | 1207.00 |
2019-07-31 | 1218.20 | 1997999 | 1224.87 | 1234.910 | 1208.18 |
2019-07-30 | 1228.00 | 1430775 | 1227.00 | 1236.910 | 1225.32 |
2019-07-29 | 1241.84 | 2069127 | 1242.50 | 1248.995 | 1230.20 |
Este exercício faz parte do curso
Intermediate Python for Finance
Instruções do exercício
- Create a mask of historical dates in the given date range.
- A mask can be used to make a selection of rows from a DataFrame.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Calculate the mask for one week
mask = (alphabet['date'] ____ start_date) & (alphabet['____'] <= end_date)
# Use the mask to get the data for one week
df = alphabet[____]
# Look at result
print(df)