gpt4 book ai didi

python - 当特定列在pandas中具有空值时选择数据

转载 作者:行者123 更新时间:2023-12-04 23:16:26 25 4
gpt4 key购买 nike

我有一个数据框,其中有2个我要过滤的日期字段,并且当任何一个日期字段为null时,都可以查看行。

ID          Date1       Date2
58844880 04/11/16 NaN
59745846 04/12/16 04/14/16
59743311 04/13/16 NaN
59745848 04/14/16 04/11/16
59598413 NaN NaN
59745921 04/14/16 04/14/16
59561199 04/15/16 04/15/16
NaN 04/16/16 04/16/16
59561198 NaN 04/17/16

它应该如下图所示
ID          Date1       Date2
58844880 04/11/16 NaN
59743311 04/13/16 NaN
59598413 NaN NaN
59561198 NaN 04/17/16

尝试过代码 df = (df['Date1'].isnull() | df['Date1'].isnull())

最佳答案

使用 boolean indexing :

mask = df['Date1'].isnull() | df['Date2'].isnull()
print (df[mask])
ID Date1 Date2
0 58844880.0 04/11/16 NaN
2 59743311.0 04/13/16 NaN
4 59598413.0 NaN NaN
8 59561198.0 NaN 04/17/16

时间:
#[900000 rows x 3 columns]
df = pd.concat([df]*100000).reset_index(drop=True)

In [12]: %timeit (df[df['Date1'].isnull() | df['Date2'].isnull()])
10 loops, best of 3: 89.3 ms per loop

In [13]: %timeit (df[df.filter(like='Date').isnull().any(1)])
10 loops, best of 3: 146 ms per loop

关于python - 当特定列在pandas中具有空值时选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40408471/

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