You can use the count
method to count the non-missing values for each column in the dataframe. Then, you can filter the columns to keep only those with less or equal to one non-missing value using boolean indexing. Finally, you can use the drop
method to drop the selected columns from the original dataframe.
Here's an example:
main.py606 chars20 lines
Output:
main.py72 chars7 lines
In this example, columns C
and E
have no non-missing values, so they are dropped. Column D
has more than one non-missing value, so it is also dropped. Columns A
and B
have one non-missing value each, so they are kept in the new dataframe.
gistlibby LogSnag