简体   繁体   English

为什么我的 .drop 删除了数据框中的所有值?

[英]Why is my .drop removing all values in my dataframe?

我一直尝试使用这行代码来删除特定列中带有 NaN 的数据行,但它一直在删除所有行:

df = df.drop(df[(df.test_variable == 'NaN')].index)

To answer the question you're really trying to ask (how to drop rows containing NaNs), use the DataFrame.dropna() function:要回答您真正想问的问题(如何删除包含 NaN 的行),请使用DataFrame.dropna()函数:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3,4),
    columns = ['A','B','C','D'])

df.loc[0, 'A'] = None
df.loc[1, 'B'] = None
df.loc[1, 'C'] = None

print(df)
print('------')
print(df.dropna())

prints:印刷:

$ py na.py
     A    B     C   D
0  NaN  1.0   2.0   3
1  4.0  NaN   NaN   7
2  8.0  9.0  10.0  11
------
     A    B     C   D
2  8.0  9.0  10.0  11

To answer the question you asked, though, this boolean check is problematic:但是,要回答您提出的问题,此布尔检查是有问题的:

df.test_variable == 'NaN'

This will literally use the string "NaN" when checking for rows that match, instead of checking for actual NaNs.这将在检查匹配的行时从字面上使用字符串"NaN" ,而不是检查实际的 NaN。 In fact, NaN is an equivalent way of saying None in Pandas, so you can use None instead of "NaN" .事实上, NaN 是 Pandas 中表示 None 的等效方式,因此您可以使用None而不是"NaN" However, simply replacing your boolean check with df.test_variable is None will still not work, because that boolean check is literally asking if df.test_variable (which will return a Pandas Series object) is None (which it is not), instead of asking which of the elements of df.test_variable is equal to None .但是,简单地用df.test_variable is None替换您的布尔检查仍然不起作用,因为该布尔检查实际上是在询问df.test_variable (它将返回一个 Pandas Series 对象)是否为None (它不是),而不是询问df.test_variable哪些元素等于None To do the element-wise check, use the isna() function of the Pandas Series object:要进行逐元素检查,请使用 Pandas Series 对象的isna()函数

Start by getting the indices of the rows containing NaN values for a particular column (in this case, A):首先获取包含特定列(在本例中为 A)的 NaN 值的行的索引:

df[df.A.isna()].index

Then pass the result to the drop() function:然后将结果传递给drop()函数:

df.drop(df[df.A.isna()].index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM