Python Pandas - 返回索引是單調遞增值(僅等於或遞增)


要返回索引是單調遞增(僅等於或遞增)的值,請使用 index.is_monotonic_increasing 屬性。

首先,匯入必需的庫 −

import pandas as pd

建立索引 −

index = pd.Index([10, 20, 20, 30, 40])

顯示索引 −

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

檢查索引是否單調遞增 −

print("\nIs the Pandas index monotonic increasing?\n",index.is_monotonic_increasing)

示例

以下是程式碼 −

import pandas as pd

# Creating the index
index = pd.Index([10, 20, 20, 30, 40])

# 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 monotonic increasing
print("\nIs the Pandas index monotonic increasing?\n",index.is_monotonic_increasing)

輸出

將生成以下程式碼 −

Pandas Index...
Int64Index([10, 20, 20, 30, 40], dtype='int64')

Array...
[10 20 20 30 40]

Is the Pandas index monotonic increasing?
True

更新日期:2021 年 10 月 13 日

508 次瀏覽

開始你的職業

完成課程獲得認證

立即開始
廣告
© . All rights reserved.