0

image of df

I would like to select a specific date e.g 2020-07-07 and get the Adj Cls and ExMA for each of the symbols. I'm new in Python and I tried using df.loc['xy'], (xy being a specific date on the datetime) and keep getting a KeyError. Any insight is greatly appreciated. Info on the df MultiIndex: 30 entries, (SNAP, 2020-07-06 00:00:00) to (YUM, 2020-07-10 00:00:00) Data columns (total 2 columns): dtypes: float64(2)

1 Answers1

0

You can use pandas.DataFrame.xs for this.

import pandas as pd
import numpy as np


df = pd.DataFrame(
    np.arange(8).reshape(4, 2), index=[[0, 0, 1, 1], [2, 3, 2, 3]], columns=list("ab")
)
print(df)
#      a  b
# 0 2  0  1
#   3  2  3
# 1 2  4  5
#   3  6  7
print(df.xs(3, level=1).filter(["a"]))
#    a
# 0  2
# 1  6
V. Ayrat
  • 2,499
  • 9
  • 10