0

I have a dataframe that looks like this:

df:
date         symbol    constituents
01-01-2022   SPY       AAPL
01-01-2022   SPY       MSFT
01-01-2022   SPY       TSLA
01-01-2022   GLD       AEM
01-01-2022   GLD       ABX
01-01-2022   GLD       KL
01-01-2022   GLD       WPM

I want to apply a tally using the groupby function to the DataFrame to get the following result

df:
ticker              AAPL   MSFT   TSLA   AEM  ABX  KL   WPM
date        symbol
01-01-2022  SPY     1      1      1      0    0    0    0
            GLD     0      0      0      0    0    0    0

I have tried the following df = d=df.groupby(["date", "symbol","constituents"])["constituents"].count() but it is not quite the shape I am looking for.

MathMan 99
  • 665
  • 1
  • 7
  • 19

0 Answers0