I have a time series pandas (df) table with many columns and with 2 indexes "date" and "ticker". I would like to use df.loc to select a specific range of dates , let say ("2000-01-03”: "2000-01-06”) and a specific “ticker” let say (“A”). In this way I would like to get all the info in the table related to these two criteria of all the other columns.
ex. of Data Frame
I tried the following
df.loc[("date", "2000-01-03”: "2000-01-06”),"A"]
Alternatively I wish to select all the tickers, I tired the following:
df.loc[("date", "2000-01-03”: "2000-01-06”),:]
both are not working. Any inside on how to use .loc in DataFrame with two index columns?
It would be great to see a sample of the dataframe, next time you submit a question, it eliminates the guess work.
Taking a look at the limited information you have provided, here are two potentials ways to solve your use case.
Approach 1 - Pandas has a great function called date_range docs. From the docs:
Approach 2 - Slice your dataframe using a boolean You can use boolean conditions to filter the DataFrame based on the desired date range and ticker. We extract the 'date' and 'ticker' levels from the multi-index using df.index.get_level_values, and then apply the conditions.
Approach 3 - Slicing without creating a multiindex