Python Pandas - 返回索引中任何元素是否為 True
要返回索引中所有元素是否都為 True,請在 Pandas 中使用 index.any() 方法。
首先,匯入所需的庫 −
import pandas as pd
建立具有某些 True(非零)和 False(零)元素的索引 −
index = pd.Index([15, 25, 0, 0, 55])
顯示索引 −
print("Pandas Index...\n",index)
如果索引中的任何元素都為 True,則返回 True:−
print("\nCheck whether any element in the index is True...\n",index.any())
示例
以下是程式碼 −
import pandas as pd # Creating the index with some True (non-zero) and False (zero) elements index = pd.Index([15, 25, 0, 0, 55]) # Display the index print("Pandas Index...\n",index) # Return a tuple of the shape of the underlying data print("\nA tuple of the shape of underlying data...\n",index.shape) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Return True if any element in the index is True print("\nCheck whether any element in the index is True...\n",index.any())
輸出
這會產生以下程式碼 −
Pandas Index... Int64Index([15, 25, 0, 0, 55], dtype='int64') A tuple of the shape of underlying data... (5,) Number of elements in the index... 5 Check whether any element in the index is True... True
廣告