0

Hi i have a df which looks like that (2 columns, the first one with year and the second one with asset class).

        YEAR    FUND_ASSET_CLASS_FOCUS
0       2011.0  Equity
1       2011.0  Equity
6       2013.0  Commodity
1066    2014.0  Equity
1128    2015.0  Equity
1309    2017.0  Mixed Allocation
1374    2022.0  Equity
1375    2022.0  Equity
1399    2017.0  Fixed Income
1426    2018.0  Equity
1449    2022.0  Equity
1474    2019.0  Equity
1507    2019.0  Equity
1530    2019.0  Fixed Income
1538    2019.0  Fixed Income
1646    2020.0  Fixed Income
1797    2021.0  Equity
1802    2021.0  Alternative
1848    2021.0  Mixed Allocation
1874    2022.0  Equity
1934    2022.0  Equity
1935    2022.0  Equity
1945    2022.0  Mixed Allocation

I would like to have a new dataframe which count for each asset class the number of time that the year appears ie:

       Equity      Alternative      Mixed Allocation      Fixed Income
2014     1            0                   0                     0
2015     1            0                   0                     0
2016     0            0                   0                     0          
2017     0            0                   1                     1
....          
jacques
  • 17
  • 4
  • 1
    Many options for that one, one idea: `pd.crosstab(df['YEAR'], df['FUND_ASSET_CLASS_FOCUS']).gt(0).astype(int)`, you can also `pivot_table` – mozway May 22 '23 at 10:46

0 Answers0