CategoricalIndex की श्रेणियों को ऑर्डर करने के लिए सेट करने के लिए, as_ordered() का उपयोग करें पंडों में विधि।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
"श्रेणियों" पैरामीटर का उपयोग करके श्रेणीबद्ध के लिए श्रेणियां सेट करें -
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], categories=["p", "q", "r", "s"])
श्रेणियां प्राप्त करें -
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
ऑर्डर की जाने वाली श्रेणियां सेट करें -
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered()) उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# Set the categories for the categorical using the "categories" parameter
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], categories=["p", "q", "r", "s"])
# Display the CategoricalIndex
print("CategoricalIndex...\n",catIndex)
# Get the categories
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
# Set the categories to be ordered
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered()) आउटपुट
यह निम्नलिखित आउटपुट देगा -
CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category') Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex ordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')