I have a df with many columns that represent the market cap of companies that compose an index. The index of the dataframe is dates. Before the company enters the index or after it leaves it, the market cap of the company in the df is zero.
I want to know the mean number of days in which the index doesn't change.
for example:
[in]:
df = pd.DataFrame(np.array([[1, 1,np.nan], [np.nan,2, 10], [1,3, 100],[4,np.nan, 100]]),
columns=['a', 'b','c'])
df
[out]:
a b c
0 1.0 1.0 NaN
1 NaN 2.0 10.0
2 1.0 3.0 100.0
3 4.0 NaN 100.0
what I want to know is how many rows have entries from the same column.
For example, row 0 has entries from columns a and b. row 1 from columns b and c. row 2 from columns a,b and c. an row 3 from columns a and c. therefore there are 4 rows with unique column combinations and 4 changes. The mean is then 1.