ในการส่งคืนสตริงของประเภทที่อนุมานจากค่า ให้ใช้ index.inferred_type ทรัพย์สินในหมีแพนด้า
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd import numpy as np
การสร้างดัชนี สำหรับ NaN เราได้ใช้ numpy library -
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])
แสดงดัชนี -
print("Pandas Index...\n",index) ส่งกลับสตริงของประเภทที่อนุมานจากค่า -
print("\nThe inferred type...\n",index.inferred_type)
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
import numpy as np
# Creating the index
# For NaN, we have used numpy library
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])
# Display the index
print("Pandas Index...\n",index)
# Return an array representing the data in the Index
print("\nArray...\n",index.values)
# Check if the index is having NaNs
print("\nIs the Pandas index having NaNs?\n",index.hasnans)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Return a string of the type inferred from the values
print("\nThe inferred type...\n",index.inferred_type) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Pandas Index... Index(['Car', 'Bike', nan, 'Car', nan, 'Ship', None, None], dtype='object') Array... ['Car' 'Bike' nan 'Car' nan 'Ship' None None] Is the Pandas index having NaNs? True The dtype object... object The inferred type... Mixed