Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन पैंडास - इंडेक्स मानों को सॉर्ट करें और उन इंडेक्स को भी लौटाएं जो इंडेक्स को सॉर्ट करेंगे


इंडेक्स मानों को सॉर्ट करने और उन इंडेक्स को भी लौटाने के लिए जो इंडेक्स को सॉर्ट करेंगे, index.sort_values() का उपयोग करें। return_indexer पैरामीटर को True पर सेट करें।

सबसे पहले, आवश्यक लाइब्रेरी इंपोर्ट करें:

import pandas as pd

पैंडास इंडेक्स बनाएं:

index = pd.Index([50, 10, 70, 95, 110, 90, 30])

पैंडास इंडेक्स प्रदर्शित करें:

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

इंडेक्स मानों को सॉर्ट करें। डिफ़ॉल्ट रूप से, आरोही क्रम में सॉर्ट करता है। "return_indexer" पैरामीटर का उपयोग करके इंडेक्स को सॉर्ट करने वाले इंडेक्स लौटाएं जिसका मान True है:

print("\nSort and also return the indices that would sort the index...\n", index.sort_values(return_indexer=True))

उदाहरण

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)
# Sort index values
# By default, sorts in Ascending order
# Return the indices to sort the index using the "return_indexer" parameter with value True
print("\nSort and also return the indices that would sort the index...\n", index.sort_values(return_indexer=True))

आउटपुट

Pandas Index... Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64')
Number of elements in the index... 7
Sort and also return the indices that would sort the index... (Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64'), array([1, 6, 0, 2, 5, 3, 4], dtype=int64))
  1. - पायथन - पैंडास इंडेक्स का अधिकतम मान प्राप्त करें

    पैंडास इंडेक्स का अधिकतम मान प्राप्त करने के लिए index.max() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd पैंडास इंडेक्स बनाएं: index = pd.Index([10, 20, 70, 40, 90, 50, 25, 30]) पैंडास इंडेक्स प्रदर्शित करें: print(Pandas Index...\n,index) अधिकतम मान प्राप्त करे

  1. - पायथन - पैंडास इंडेक्स का न्यूनतम मान प्राप्त करें

    पैंडास इंडेक्स का न्यूनतम मान प्राप्त करने के लिए index.min() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd पैंडास इंडेक्स बनाएं: index = pd.Index([10.5, 20.4, 40.5, 25.6, 5.7, 6.8, 30.8, 50.2]) पैंडास इंडेक्स प्रदर्शित करें: print(Pandas Index...\n,index) न्यूनतम

  1. - पायथन पैंडास - एक कॉलम का अधिकतम मान खोजें और उसकी संबंधित पंक्ति मान लौटाएं

    पैंडास में एक कॉलम का अधिकतम मान खोजने और उसकी संबंधित पंक्ति मान लौटाने के लिए, हम df.loc[df[col].idxmax()] का उपयोग कर सकते हैं। आइए एक उदाहरण लेकर इसे बेहतर ढंग से समझें। चरण दो-आयामी, आकार-परिवर्तनीय, संभावित विषम सारणीबद्ध डेटा, df बनाएं। इनपुट डेटाफ्रेम, df प्रिंट करें। एक वेरिएबल, col इनिशि