इंडेक्स की सॉर्टेड कॉपी लौटाने के लिए, पैंडास में index.sort_values() मेथड का उपयोग करें। सबसे पहले, आवश्यक लाइब्रेरी इंपोर्ट करें:
import pandas as pd
पैंडास इंडेक्स बनाएं:
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
पैंडास इंडेक्स प्रदर्शित करें:
print("Pandas Index...\n", index)
इंडेक्स मानों को सॉर्ट करें। डिफ़ॉल्ट रूप से, आरोही क्रम में सॉर्ट करता है:
print("\nSort the index values...\n", index.sort_values())
उदाहरण
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
# By default, sorts in Ascending order
print("\nSort the index values...\n", index.sort_values())
आउटपुट
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... Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64')