gpt4 book ai didi

python - 计算 Pandas df 中的非空值

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:08 25 4
gpt4 key购买 nike

我有一个包含大约 70 列的数据集,如下所示:

ID_number   Meeting1    Meeting2    Meeting3    Meeting4    Meeting5    Comments    …
123456789 9/15/2015 1/8/2016 4/27/2016 NaN NaN text text …
987654321 9/22/2016 NaN 2/25/2017 NaN NaN text text …
456789123 10/1/2015 11/30/2015 NaN NaN NaN text text …

我想创建一个附加列 (meeting_count),其中包含 Meeting1-Meeting5 列的非空值计数,对应 ID_number。

通常我会使用 SQL 并执行如下操作:

select
Meeting1,
Meeting2,
Meeting3,
Meeting4,
Meeting5,
(
select count(*)
from (values (Meeting1), (Meeting2), (Meeting3), (Meeting4), (Meeting5)) as v(col)
where v.col is not null
) as meeting_count
from Table

但如果在 Python 中有一种相对简单的方法来做到这一点,我宁愿那样做。

最佳答案

试试这个

df['meeting_count'] = df.filter(regex=r'^Meeting').notnull().sum(axis=1)

演示:

In [8]: df
Out[8]:
ID_number Meeting1 Meeting2 Meeting3 Meeting4 Meeting5 Comments
123456789 9/15/2015 1/8/2016 4/27/2016 NaN NaN text text
987654321 9/22/2016 NaN 2/25/2017 NaN NaN text text
456789123 10/1/2015 11/30/2015 NaN NaN NaN text text

In [9]: df['meeting_count'] = df.filter(regex=r'^Meeting').notnull().sum(axis=1)

In [10]: df
Out[10]:
ID_number Meeting1 Meeting2 Meeting3 Meeting4 Meeting5 Comments meeting_count
123456789 9/15/2015 1/8/2016 4/27/2016 NaN NaN text text 3
987654321 9/22/2016 NaN 2/25/2017 NaN NaN text text 2
456789123 10/1/2015 11/30/2015 NaN NaN NaN text text 2

关于python - 计算 Pandas df 中的非空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com