简体   繁体   English

对于在 Pandas 中不是 NaN 的循环查找值

[英]For loop finding values that are not NaN in Pandas

I'm attempting to create a statement where if an object in my dataframe is notna and has a Book_Status of True, then continue on to the next task.我正在尝试创建一个语句,如果我的 dataframe 中的 object 不存在并且 Book_Status 为 True,则继续执行下一个任务。 However, when I attempt to do this I get "float" object has no attribute "notna".但是,当我尝试这样做时,我得到“浮动”object 没有属性“notna”。

I've looked into np.where, but that seems to be used to create a column?我查看了 np.where,但这似乎是用来创建列的? For loop using np.where 使用 np.where 循环

Example dataframe示例 dataframe

name       quote id       Book_Status
Park       foobar300      False
Bus        NaN            False
Car        NaN            False

And here is what my code is that's giving me my error这就是我的代码,它给了我我的错误

def BookEvent(df):
    y = 0
    for i in range(len(df_parking.index)):
        if df['quote id'][y].notna() & df['Book_Status'][y] == False:
          # Then do something unrelated to this df

In your solution working with scalar, so need pd.notna and instead & use and , but this loop solution is slow:在您使用标量的解决方案中,因此需要pd.notna而不是&使用and ,但是这个循环解决方案很慢:

if pd.notna(df['quote id'][y]) and df['Book_Status'][y] == False:

But in pandas is better/ faster working with masks like:但是在 pandas 中使用以下面具更好/更快:

mask = df['quote id'].notna() & ~df['Book_Status']
df['new'] = np.where(mask, 10, 20)

print (df)
   name   quote id  Book_Status  new
0  Park  foobar300        False   10
1   Bus        NaN        False   20
2   Car        NaN        False   20

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

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