I have a DataFrame that looks like this:
0
[0, 0, 0, 0, 0] 0.0
[1, 0, 0, 0, 0] 1.0
...
[0, 1, 1, 1, 1] 0.0
[1, 1, 1, 1, 1] 1.0
I would like to subset it to the indices that contain a given number of 1s.
The condition might look something like
lambda x: np.count_nonzero(x) == n
for n number of 1s.
I know that subsetting by condition can be done somehow with .loc, and there are many answers that explain how to do it when the condition is set on columns. I didn't find anything that explains how to do it if the condition is on the indices themselves.
I tried this:
rounded.loc[np.count_nonzero(rounded.index) == n]
But no luck. I am unsure whether something really obvious is escaping me.
I could just make a new column that has the indices as values and subset based on that, but I was wondering whether there is a more elegant solution.