Python Pandas——计算观测值
要计算观测值,首先使用 groupby(),然后对结果使用 count()。首先,导入所需的库 -
dataFrame = pd.DataFrame({'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'],'Product Category': ['Computer', 'Mobile Phone', 'Electronics','Electronics', 'Computer', 'Mobile Phone'],'Quantity': [10, 50, 10, 20, 25, 50]})
分组有重复值的列 -
group = dataFrame.groupby("Product Category")
获取计数 -
group.count()
示例
以下为代码 -
import pandas as pd # create a dataframe dataFrame = pd.DataFrame({'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'],'Product Category': ['Computer', 'Mobile Phone', 'Electronics','Electronics', 'Computer', 'Mobile Phone'],'Quantity': [10, 50, 10, 20, 25, 50]}) # dataframe print"Dataframe...\n",dataFrame # count the observations group = dataFrame.groupby("Product Category") print"\nResultant DataFrame...\n",group.count()
输出
这将生成以下输出 -
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 Resultant DataFrame... Product Name Quantity Product Category Computer 2 2 Electronics 2 2 Mobile Phone 2 2
广告