从 Pandas 数据框中删除 NaT 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24546073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:13:05  来源:igfitidea点击:

Get rid of NaT values from pandas dataframe

pythonpandas

提问by user3527975

I have a dataframe that looks like shown below

我有一个如下所示的数据框

                               mean
comp_name  date                      
Appdynamics 2012-05-01 00:18:15.910000
            2012-05-01             NaT
            2012-05-01             NaT
            2012-05-02 00:20:12.145200
            2012-05-02             NaT
            2012-05-02             NaT

Here the comp_name and date form multiindex. I want to get rid of the NaT values and obtain only those rows where the mean(timedelta64) is not NaT.

这里的 comp_name 和 date 构成多索引。我想去掉 NaT 值,只获取那些均值(timedelta64)不是 NaT 的行。

                               mean
comp_name  date                      
Appdynamics 2012-05-01 00:18:15.910000
            2012-05-02 00:20:12.145200

Any ideas on this?

对此有何想法?

回答by exp1orer

pandas.notnull()takes a series and returns a Boolean series which is True where the input series is not null (None, np.NaN, np.NaT). Then you can slice a dataframe by the Boolean series:

pandas.notnull()接受一个系列并返回一个布尔系列,该系列为 True,其中输入系列不为空(无、np.NaN、np.NaT)。然后您可以通过布尔系列对数据帧进行切片:

df[pandas.notnull(df['mean'])]