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

Python - ตรวจสอบว่า Pandas Index เป็นชนิด floating

การตรวจสอบว่า Index เป็นชนิดข้อมูล floating

ใช้เมธอด index.is_floating() ตรวจสอบว่า Index มีเฉพาะค่าทศนิยม (float) เท่านั้น

ขั้นตอน

  1. นำเข้า pandas
  2. สร้าง Index แบบทศนิยม
  3. แสดง Index
  4. ใช้ is_floating() ตรวจสอบ

ตัวอย่างโค้ด

import pandas as pd

# สร้าง Index แบบทศนิยม
index = pd.Index([5.7, 6.8, 10.5, 20.4, 25.6, 30.8, 40.5, 50.2])

# แสดง Index
print("Pandas Index...\n", index)

# แสดงจำนวนองค์ประกอบและ dtype
print("\nNumber of elements in the index...\n", index.size)
print("\nThe dtype object...\n", index.dtype)

# ตรวจสอบว่าเป็น floating เท่านั้น
print("\nIndex values only consists of floats?\n", index.is_floating())

ผลลัพธ์

Pandas Index... Float64Index([5.7, 6.8, 10.5, 20.4, 25.6, 30.8, 40.5, 50.2], dtype='float64')
Number of elements in the index... 8
The dtype object... float64
Index values only consists of floats? True

ความแตกต่างจาก is_numeric

is_floating() ตรวจสอบเฉพาะ float เท่านั้น ไม่รวม int หรือ NaN ในขณะที่ is_numeric() รวม int, float, NaN