इंडेक्स ऑब्जेक्ट से सापेक्ष आवृत्ति वापस करने के लिए, index.value_counts() . का उपयोग करें पैरामीटर के साथ विधि सामान्य करें सच . के रूप में ।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
पांडा इंडेक्स बनाना -
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
पांडा सूचकांक प्रदर्शित करें -
print("Pandas Index...\n",index) value_counts() का उपयोग करके अद्वितीय मानों की गणना प्राप्त करें। सापेक्ष आवृत्ति प्राप्त करने के लिए पैरामीटर "सामान्यीकृत करें" को True पर सेट करें -
print("\nGet the relative frequency by dividing all values by the sum of values...\n", index.value_counts(normalize=True)) उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# Creating Pandas index
index = pd.Index([50, 10, 70, 110, 90, 50, 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)
# Get the count of unique values using value_counts()
# Set the parameter "normalize" to True to get the relative frequency
print("\nGet the relative frequency by dividing all values by the sum of values...\n", index.value_counts(normalize=True)) आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
Pandas Index... Int64Index([50, 10, 70, 110, 90, 50, 110, 90, 30], dtype='int64') Number of elements in the index... 9 The dtype object... int64 Get the relative frequency by dividing all values by the sum of values... 50 0.222222 110 0.222222 90 0.222222 10 0.111111 70 0.111111 30 0.111111 dtype: float64