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

Python Pandas CategoricalIndex - डिक्ट-जैसी नई श्रेणियों के साथ श्रेणियों का नाम बदलें

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

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

import pandas as pd

CategoricalIndex बनाएं

"ordered" पैरामीटर का उपयोग करके श्रेणी को क्रमबद्ध मानें।

catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])

CategoricalIndex प्रदर्शित करें

print("CategoricalIndex...\n", catIndex)

डिक्ट-जैसी नई श्रेणियों के साथ नाम बदलें

पुरानी श्रेणियों को बदलने वाली नई डिक्ट-जैसी श्रेणियां सेट करें।

print("\nCategoricalIndex after renaming categories...\n", catIndex.rename_categories({'p': 5, 'q': 10, 'r': 15, 's': 20}))

पूरा उदाहरण

import pandas as pd

# श्रेणियां सेट करें और क्रमबद्ध मानें
catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"])

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

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

# डिक्ट-जैसी नई श्रेणियों के साथ नाम बदलें
print("\nCategoricalIndex after renaming categories...\n", catIndex.rename_categories({'p': 5, 'q': 10, 'r': 15, 's': 20}))

आउटपुट

CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')
Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object')
CategoricalIndex after renaming categories... CategoricalIndex([5, 10, 15, 20, 5, 10, 15, 20], categories=[5, 10, 15, 20], ordered=True, dtype='category')

व्याख्या: डिक्ट {'p': 5, 'q': 10, 'r': 15, 's': 20} प्रत्येक पुरानी श्रेणी को नए मान से मैप करता है।

  1. - Python Pandas - लेबल की सूची हटाकर नया इंडेक्स बनाएं

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

  1. - Python Pandas - एकाधिक इंडेक्स तत्व हटाकर नया इंडेक्स बनाएं

    परिचय पैंडास इंडेक्स से एकाधिक स्थितियों (पोजीशन) के आधार पर तत्व हटाकर नया इंडेक्स बनाने के लिए index.delete() मेथड का उपयोग करें। इसमें हटाए जाने वाले इंडेक्स की सूची पास करें। आवश्यक लाइब्रेरी आयात करें import pandas as pd इंडेक्स बनाएं index = pd.Index([15, 25, 35, 45, 55, 75, 95]) इंडेक्स प्

  1. - पायथन - पैंडास DataFrame.rename()

    पैंडास में डेटाफ्रेम कॉलम का नाम बदलना काफी सरल है। आपको बस rename() विधि का उपयोग करना है और उस कॉलम नाम को पास करना है जिसे आप बदलना चाहते हैं और नया कॉलम नाम। आइए एक उदाहरण लें और देखें कि यह कैसे किया जाता है। चरण दो-आयामी, आकार-परिवर्तनीय, संभावित विषम सारणीबद्ध डेटा, df बनाएं। इनपुट डेटाफ्रे