I import the following quotes into a DataFrame.
import pandas as pd
import numpy as np
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)
# download dataframe
datos = pdr.get_data_yahoo("SPY", start="2000-01-01", end="2021-04-30")
datos.shape[0]
The statement datos.shape[0]
returns the number of records in the DataFrame, which in this case is 5366. Next I convert the DataFrame to a dictionary and try to get it to return the number of elements in the dictionary.
data = datos.to_dict()
len(data)
It returns me 6, which is wrong. What is my fault? I will appreciate help.
The error is understanding, the returned result is correct , when you use
len()
on a dictionary it returns the number of keys that said dictionary has (which correspond to the columns of yourDataFrame
) and you have six columns/keys.Bonuses
If you want to work on a data table (making counts, averages, extracting lengths) I recommend you stick with the format
DataFrame
since a dictionary will be more complicated to work with.I would only use the dictionary in situations of memory or time constraints.