प्रत्येक समूह में पंक्तियों की संख्या गिनने के लिए group.size() का उपयोग करें। आवश्यक लाइब्रेरी आयात करें:
import pandas as pd
एक डेटाफ्रेम बनाएं:
dataFrame = pd.DataFrame({'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'],
'Quantity': [10, 50, 10, 20, 25, 50],
'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone']})
कॉलम द्वारा समूहित करें:
dataFrame.groupby(["Product Category", "Quantity"])
अब, प्रत्येक समूह में पंक्तियों की संख्या प्राप्त करने के लिए समूह आकार गिनें।
उदाहरण
निम्नलिखित पूर्ण कोड है:
import pandas as pd
# create a dataframe
dataFrame = pd.DataFrame({'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'],
'Quantity': [10, 50, 10, 20, 25, 50],
'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone']})
# dataframe
print("Dataframe...\n", dataFrame)
# grouping columns
new_group = dataFrame.groupby(["Product Category", "Quantity"])
# group size
new_group = new_group.size()
# dataframe
print("\nUpdated Dataframe...\n", new_group)
आउटपुट
यह निम्नलिखित आउटपुट देगा:
Dataframe...
Product Category Product Name Quantity
0 Computer Keyboard 10
1 Mobile Phone Charger 50
2 Electronics SmartTV 10
3 Electronics Camera 20
4 Computer Graphic Card 25
5 Mobile Phone Earphone 50
Updated Dataframe...
Product Category Quantity
Computer 10 1
25 1
Electronics 10 1
20 1
Mobile Phone 50 2
dtype: int64