Python Pandas - 返回索引是否為單調遞減(僅相等或遞減)值


如要返回索引是否為單調遞減(僅相等或遞減)的值,請使用 index.is_monotonic_decreasing 屬性。

首先,匯入所需的庫 −

import pandas as pd

建立索引 −

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

顯示索引 −

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

檢查索引是否單調遞減 −

print("\nIs the Pandas index monotonic decreasing?\n",index.is_monotonic_decreasing)

示例

程式碼如下 −

import pandas as pd

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

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

輸出

這將生成以下程式碼 −

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

Array...
[50 40 30 30 30]

Is the Pandas index monotonic decreasing?
True

更新於: 13-10-2021

153 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始
廣告