मान लें, आपके पास एक सीरीज है और सॉर्टेड विशिष्ट मानों के साथ न्यूमेरिक इंडेक्स सरणी:
Sorted distinct values - numeric array index [2 3 0 3 2 1 4] ['apple' 'kiwi' 'mango' 'orange' 'pomegranate']
इसे हल करने के लिए, हम नीचे दिए गए चरणों का पालन करेंगे:
समाधान
गैर-यूनिक तत्वों की सूची के अंदर pd.factorize() फ़ंक्शन लागू करें और इसे index, index_value के रूप में सेव करें।
index, unique_value = pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'])
इंडेक्स और एलिमेंट प्रिंट करें। परिणाम बिना विशिष्ट मानों और इसके इंडेक्स की सॉर्टिंग के प्रदर्शित होता है
सूची एलिमेंट के अंदर pd.factorize() लागू करें और sort=True सेट करें फिर इसे sorted_index, unique_value के रूप में सेव करें
sorted_index, unique_value = pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'], sort=True)
अंत में न्यूमेरिक इंडेक्स और विशिष्ट मान प्रिंट करें
उदाहरण
बेहतर समझ के लिए नीचे दिए गए कोड को देखें:
import pandas as pd
index, unique_value = pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'])
print("Without sorting of distinct values - numeric array index")
print(index)
print(unique_value)
print("Sorted distinct values - numeric array index")
sorted_index, unique_value = pd.factorize(['mango','orange','apple','orange','mango','kiwi','pomegranate'], sort=True)
print(sorted_index)
print(unique_value)
आउटपुट
Without sorting of distinct values - numeric array index [0 1 2 1 0 3 4] ['mango' 'orange' 'apple' 'kiwi' 'pomegranate'] Sorted distinct values - numeric array index [2 3 0 3 2 1 4] ['apple' 'kiwi' 'mango' 'orange' 'pomegranate']