Python:如何删除特定列为空/NaN的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46091924/
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-08-19 17:28:04  来源:igfitidea点击:

Python: How to drop a row whose particular column is empty/NaN?

pythonpandasdataframe

提问by Haroon S.

I have a csv file. I read it:

我有一个 csv 文件。我读了它:

import pandas as pd
data = pd.read_csv('my_data.csv', sep=',')
data.head()

It has output like:

它有如下输出:

id    city    department    sms    category
01    khi      revenue      NaN       0
02    lhr      revenue      good      1
03    lhr      revenue      NaN       0

I want to remove all the rows where smscolumn is empty/NaN. What is efficient way to do it?

我想删除sms列为空/NaN 的所有行。什么是有效的方法?

回答by jezrael

Use dropnawith parameter subsetfor specify column for check NaNs:

dropna与参数subset一起使用以指定用于检查NaN的列:

data = data.dropna(subset=['sms'])
print (data)
   id city department   sms  category
1   2  lhr    revenue  good         1

Another solution with boolean indexingand notnull:

使用boolean indexing和的另一种解决方案notnull

data = data[data['sms'].notnull()]
print (data)
   id city department   sms  category
1   2  lhr    revenue  good         1

Alternative with query:

替代query

print (data.query("sms == sms"))
   id city department   sms  category
1   2  lhr    revenue  good         1

Timings

时间安排

#[300000 rows x 5 columns]
data = pd.concat([data]*100000).reset_index(drop=True)

In [123]: %timeit (data.dropna(subset=['sms']))
100 loops, best of 3: 19.5 ms per loop

In [124]: %timeit (data[data['sms'].notnull()])
100 loops, best of 3: 13.8 ms per loop

In [125]: %timeit (data.query("sms == sms"))
10 loops, best of 3: 23.6 ms per loop

回答by Thijs D

You can use the method dropnafor this:

您可以dropna为此使用该方法:

data.dropna(axis=0, subset=('sms', ))

See the documentationfor more details on the parameters.

有关参数的更多详细信息,请参阅文档

Of course there are multiple ways to do this, and there are some slight performance differences. Unless performance is critical, I would prefer the use of dropna()as it is the most expressive.

当然有多种方法可以做到这一点,并且存在一些细微的性能差异。除非性能很关键,否则我更喜欢使用 ,dropna()因为它最具表现力。

import pandas as pd
import numpy as np

i = 10000000

# generate dataframe with a few columns
df = pd.DataFrame(dict(
    a_number=np.random.randint(0,1e6,size=i),
    with_nans=np.random.choice([np.nan, 'good', 'bad', 'ok'], size=i),
    letter=np.random.choice(list('abcdefghijklmnop'), size=i))
                 )

# using notebook %%timeit
a = df.dropna(subset=['with_nans'])
#1.29 s ± 112 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# using notebook %%timeit
b = df[~df.with_nans.isnull()]
#890 ms ± 59.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# using notebook %%timeit
c = df.query('with_nans == with_nans')
#1.71 s ± 100 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)