I have a pandas dataframe from which I want to create a new dataframe by applying a filter based on the count function such that only those columns should be selected whose count is equal to a specified number.
For example in the dataframe below:
month_end Col A Col B Col C Col D
200703 NaN NaN NaN NaN
200704 0.084 0.152 0.142 0.0766
200705 0.124 0.123 0.020 NaN
200706 NaN 0.191 0.091 0.149
200707 -0.136 0.047 0.135 -0.127
If my_variable = 4, then df1 should only contain Col B and Col D alongwith the index month_end.
How do I do this?
countmethod that will give you a series where the index is the names of the columns and the values are the number of non-null results in that column.NaNvalues, but be specific.dataframe.count()