Python - 顯示 Pandas 索引中非 NA 條目


要檢視 Pandas 索引中哪些條目不是 NA,請使用 index.notna() 方法。首先,匯入必需的庫 -

import pandas as pd
import numpy as np

使用一些 NaN 值建立 Pandas 索引 -

index = pd.Index([5, 65, np.nan, 17, 75, np.nan])

顯示 Pandas 索引 -

print("Pandas Index...\n",index)

顯示 Pandas 索引中哪些條目不是 NA。對於非 NA 條目返回 True -

print("\nCheck which entries are not-NA...\n", index.notna())

範例

以下是程式碼 -

import pandas as pd
import numpy as np

# Creating Pandas index with some NaN values
index = pd.Index([5, 65, np.nan, 17, 75, np.nan])

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# Show which entries in a Pandas index are not-NA
# Return True for non-NA entries
print("\nCheck which entries are not-NA...\n", index.notna())

輸出

將生成以下輸出 -

Pandas Index...
Float64Index([5.0, 65.0, nan, 17.0, 75.0, nan], dtype='float64')

Number of elements in the index...
6

The dtype object...
float64

Check which entries are not-NA...
[ True True False True True False]

更新於:2021 年 10 月 13 日

131 次瀏覽

開啟您的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.