Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python - แสดงรายการใดในดัชนี Pandas ไม่ใช่ NA


หากต้องการแสดงว่ารายการใดในดัชนี Pandas ไม่ใช่ NA ให้ใช้ index.notna() กระบวนการ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd
import numpy as np

การสร้างดัชนี Pandas ด้วยค่า NaN -

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

แสดงดัชนีหมีแพนด้า −

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

แสดงว่ารายการใดในดัชนี Pandas ไม่ใช่-NA คืนค่า True สำหรับรายการที่ไม่ใช่ NA -

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]