Để trả về một chuỗi kiểu được suy ra từ các giá trị, hãy sử dụng index.inferred_type tài sản ở Pandas.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd import numpy as np
Tạo chỉ mục. Đối với NaN, chúng tôi đã sử dụng thư viện numpy -
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship', None, None])
Hiển thị chỉ mục -
print("Pandas Index...\n",index) Trả về một chuỗi kiểu được suy ra từ các giá trị -
print("\nThe inferred type...\n",index.inferred_type)
Ví dụ
Sau đây là mã -
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) Đầu ra
Điều này sẽ tạo ra mã sau -
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