Sort rows of a dataframe in descending order of NaN counts

前端 未结 4 2026
栀梦
栀梦 2021-01-13 12:41

I\'m trying to sort the following Pandas DataFrame:

         RHS  age  height  shoe_size  weight
0     weight  NaN     0.0        0.0     1.0
1  shoe_size  N         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 13:05

    Using df.sort_values and loc based accessing.

    df = df.iloc[df.isnull().sum(1).sort_values(ascending=0).index]
    print(df)
    
             RHS  age  height  shoe_size  weight
    1  shoe_size  NaN     0.0        1.0     NaN
    2  shoe_size  3.0     0.0        0.0     NaN
    0     weight  NaN     0.0        0.0     1.0
    4        age  3.0     0.0        0.0     1.0
    3     weight  3.0     0.0        0.0     1.0
    

    df.isnull().sum(1) counts the NaNs and the rows are accessed based on this sorted count.


    @ayhan offered a nice little improvement to the solution above, involving pd.Series.argsort:

    df = df.iloc[df.isnull().sum(axis=1).mul(-1).argsort()]
    print(df)
    
             RHS  age  height  shoe_size  weight 
    1  shoe_size  NaN     0.0        1.0     NaN           
    0     weight  NaN     0.0        0.0     1.0           
    2  shoe_size  3.0     0.0        0.0     NaN           
    3     weight  3.0     0.0        0.0     1.0           
    4        age  3.0     0.0        0.0     1.0            
    

提交回复
热议问题