pandas 如何选择groupby中空值最少的组?

k4emjkb1  于 2023-03-28  发布在  其他
关注(0)|答案(2)|浏览(99)

示例:

row_number |id |firstname | middlename | lastname |
0          | 1 | John     | NULL       | Doe      |
1          | 1 | John     | Jacob      | Doe      |
2          | 2 | Alison   | Marie      | Smith    |
3          | 2 | NULL     | Marie      | Smith    |
4          | 2 | Alison   | Marie      | Smith    |

我试图弄清楚如何groupby id,然后为每个groupby获取具有最少NULL值的行,删除包含最少NULL值的任何额外行都可以(例如,删除row_number 4,因为它将row_number 2与id=2的最少NULL值联系起来)
本例的答案是row_numbers 1和2
最好是ANSI SQL,但我可以翻译其他语言(如python与pandas),如果你能想到一种方法来做
编辑:为平局打破的情况增加了一行。

jdzmm42g

jdzmm42g1#

如果你想这样做Pandas,你可以这样做:

df[df.assign(NC = df.isnull().sum(1)).groupby('id')['NC'].transform(lambda x: x == x.min())]

输出:

row_number  id firstname middlename lastname
1           1   1      John      Jacob      Doe
2           2   2    Alison      Marie    Smith

决胜局:
添加一行:

df.loc[4,['row_number','id','firstname','middlename','lastname']] = ['4',2,'Mary','Maxine','Maxwell']

然后使用groupbytransformidxmin

df[df.index == df.assign(NC = df.isnull().sum(1)).groupby('id')['NC'].transform('idxmin')]

输出:

row_number id firstname middlename lastname
1          1  1      John      Jacob      Doe
2          2  2    Alison      Marie    Smith
jm81lzqq

jm81lzqq2#

哦,你想要null值最少的行。我建议:

select t.*
from (select t.*,
             dense_rank() over (order by (case when firstname is null then 1 else 0 end) + 
                                         (case when middlename is null then 1 else 0 end) + 
                                         (case when lastname is null then 1 else 0 end)
                               ) as seqnum

      from t
     ) t
where seqnum = 1;

这是ANSI标准的SQL。

相关问题