【问题标题】:Doing the opposite of dropna in pandas dataframe [duplicate]在熊猫数据框中做与dropna相反的事情[重复]
【发布时间】:2020-09-18 13:24:30
【问题描述】:

我确信这很容易做到,但我似乎无法找到堆栈溢出的答案。我有以下数据框。

# Import pandas library 
import pandas as pd
import numpy as np

# initialize list of lists 
data = [['tom', 10,1], ['nick', 15,np.nan], ['juli', 14,1], ['mick', 15,np.nan], ['james', 18,1], ['nathan', 15,np.nan], ['jason', 17,1]
       , ['ted', 14,np.nan], ['ben',16 ,1], ['dom', 22,1]] 

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age','Excuse']) 
print(df)

    Name  Age  Excuse
0     tom   10     1.0
1    nick   15     NaN
2    juli   14     1.0
3    mick   15     NaN
4   james   18     1.0
5  nathan   15     NaN
6   jason   17     1.0
7     ted   14     NaN
8     ben   16     1.0
9     dom   22     1.0

我希望删除所有具有excuse 的行以便我得到:

   Name  Age  Excuse

1    nick   15     NaN
3    mick   15     NaN
5  nathan   15     NaN
7     ted   14     NaN

有什么想法吗?有与df = df.dropna(subset=['Excuse'])相反的功能吗?谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    请尝试

    df[df.Excuse.isna()]
    

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 2021-02-04
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      相关资源
      最近更新 更多