Python Pandas - 返回已按降序排列的、經過排序的索引副本
要返回排序後的索引副本,可在 Pandas 中使用 index.sort_values() 方法。ascending 引數設定為 false。
首先,匯入所需的庫 –
import pandas as pd
建立 Pandas 索引 –
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
顯示 Pandas 索引 –
print("Pandas Index...\n",index)
排序索引值。要按降序排列值,將“ascending”引數設定為“false” –
print("\nSort the index values in descending order...\n",index.sort_values(ascending=False))
示例
以下為程式碼 –
import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 95, 110, 90, 30]) # Display the Pandas index print("Pandas Index...\n",index) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # Sort index values # To sort values in Descending order, set the "ascending" parameter to "False" print("\nSort the index values in descending order...\n",index.sort_values(ascending=False))
輸出
這將產生以下輸出 –
Pandas Index... Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64') Number of elements in the index... 7 The dtype object... int64 Sort the index values in descending order... Int64Index([110, 95, 90, 70, 50, 30, 10], dtype='int64')
廣告