2021年4月29日星期四

Filtering pandas dataframe on condition over NaN

I have a datetime dataframe in pandas like this:

                 date  value1  value2 name  0  2020-08-27 07:30:00    28.0    27.0    A  1  2020-08-27 08:00:00    28.2    27.0    A  2  2020-08-27 09:00:00    NaN     27.5    A  3  2020-08-27 09:30:00    29.0     NaN    A  4  2020-08-27 10:30:00    NaN      NaN    A  5  2020-08-27 11:00:00    29.8    27.0    A  6  2020-08-27 11:30:00    30.0    27.0    A  7  2020-08-27 12:00:00    30.0    27.0    A  8  2020-08-27 12:30:00    30.0    27.0    A  9  2020-08-27 13:30:00    30.0    27.0    A  10  2020-08-27 07:30:00    28.0    27.0    B  11  2020-08-27 08:00:00    28.2    27.0    B  12  2020-08-27 09:00:00    NaN     27.5    B  13  2020-08-27 09:30:00    29.0     NaN    B  14  2020-08-27 10:30:00    NaN      NaN    B  15  2020-08-27 11:00:00    29.8     NaN    B  16  2020-08-27 11:30:00    30.0    27.0    B  17  2020-08-27 12:00:00    30.0    27.0    B  18  2020-08-27 12:30:00    30.0    27.0    B  19  2020-08-27 13:30:00    30.0    27.0    B  

I wish to remove entry for all name for which number of NaN in any column is 3 or more. I am able to calculate NaN in each column.

df.drop('name', 1).isna().groupby(df.name, sort=False).sum().reset_index()  

How can I use this to filter df:

My expected output is:

               date  value1  value2 name  0  2020-08-27 07:30:00    28.0    27.0    A  1  2020-08-27 08:00:00    28.2    27.0    A  2  2020-08-27 09:00:00    NaN     27.5    A  3  2020-08-27 09:30:00    29.0     NaN    A  4  2020-08-27 10:30:00    NaN      NaN    A  5  2020-08-27 11:00:00    29.8    27.0    A  6  2020-08-27 11:30:00    30.0    27.0    A  7  2020-08-27 12:00:00    30.0    27.0    A  8  2020-08-27 12:30:00    30.0    27.0    A  9  2020-08-27 13:30:00    30.0    27.0    A  
https://stackoverflow.com/questions/67328047/filtering-pandas-dataframe-on-condition-over-nan April 30, 2021 at 12:07PM

没有评论:

发表评论