使用 Python 編寫程式,查詢給定序列中 NaN 值的索引。
輸入 -
假設你有一個序列,
0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaN
輸出 -
並且,NaN 索引的結果是,
index is 3 index is 5
解決方案
為了解決這個問題,我們將遵循以下步驟 -
定義一個序列。
建立 for 迴圈並訪問所有元素,設定 if 條件以檢查 isnan()。最後列印索引位置。如下所示:
for i,j in data.items():
if(np.isnan(j)):
print("index is",i)示例
讓我們看看下面的實現,以便更好地理解。
import pandas as pd
import numpy as np
l = [1,2,3,np.nan,4,np.nan]
data = pd.Series(l)
print(data)
for i,j in data.items():
if(np.isnan(j)):
print("index is",i)輸出
0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaN dtype: float64 index is 3 index is 5
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP