[英]For loop finding values that are not NaN in Pandas
我正在嘗試創建一個語句,如果我的 dataframe 中的 object 不存在並且 Book_Status 為 True,則繼續執行下一個任務。 但是,當我嘗試這樣做時,我得到“浮動”object 沒有屬性“notna”。
我查看了 np.where,但這似乎是用來創建列的? 使用 np.where 循環
示例 dataframe
name quote id Book_Status
Park foobar300 False
Bus NaN False
Car NaN False
這就是我的代碼,它給了我我的錯誤
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
在您使用標量的解決方案中,因此需要pd.notna
而不是&
使用and
,但是這個循環解決方案很慢:
if pd.notna(df['quote id'][y]) and df['Book_Status'][y] == False:
但是在 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.