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

Python Pandas - CategoricalIndex की श्रेणियों को क्रमबद्ध सेट करें

Pandas में CategoricalIndex की श्रेणियों को क्रमबद्ध सेट करने के लिए as_ordered() मेथड का उपयोग करें।

आवश्यक लाइब्रेरी इम्पोर्ट करें

import pandas as pd

CategoricalIndex बनाएं

"categories" पैरामीटर का उपयोग करके श्रेणियां सेट करें।

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

# श्रेणियां सेट करें
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], categories=["p", "q", "r", "s"])

# CategoricalIndex प्रदर्शित करें
print("CategoricalIndex...\n", catIndex)

# श्रेणियां प्राप्त करें
print("\nDisplaying Categories from CategoricalIndex...\n", catIndex.categories)

# श्रेणियों को क्रमबद्ध सेट करें
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')

व्याख्या: as_ordered() मेथड श्रेणियों को क्रमबद्ध (ordered=True) सेट करके नया CategoricalIndex लौटाता है।

  1. - Python Pandas - इंडेक्स का ट्रांसपोज़ लौटाएं

    परिचय पैंडास इंडेक्स का ट्रांसपोज़ (transpose) प्राप्त करने के लिए index.T प्रॉपर्टी का उपयोग करें। चूंकि इंडेक्स 1-आयामी होता है, इसका ट्रांसपोज़ स्वयं ही होता है। आवश्यक लाइब्रेरी आयात करें import pandas as pd इंडेक्स बनाएं index = pd.Index([Car, Bike, Truck, Ship, Airplane]) इंडेक्स प्रदर्शित

  1. - Python Pandas - मास्क के साथ सेट किए गए मानों का नया इंडेक्स लौटाएं

    परिचय बूलियन मास्क के आधार पर इंडेक्स मानों को बदलकर नया इंडेक्स प्राप्त करने के लिए index.putmask() मेथड का उपयोग करें। आवश्यक लाइब्रेरी आयात करें import pandas as pd पैंडास इंडेक्स बनाएं index = pd.Index([5, 65, 10, 17, 75, 40]) पैंडास इंडेक्स प्रदर्शित करें print(पैंडास इंडेक्स...\n, index) 3

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

    पैंडास को सेट में टाइपकास्ट करने के लिए, set() का उपयोग करें। सबसे पहले, आइए एक डेटाफ्रेम बनाएं - dataFrame = pd.DataFrame( { EmpName: [John, Ted, Jacob, Scarlett, Ami, Ted, Scarlett], Zone: [North, South, South, East, West, East, North] } ) पैंडास को सेट में टाइपकास्ट करें