求每款产品主的流价格及其占比

方法一:

点击查看代码
import pandas as pd
import numpy as np
df=pd.read_excel(r'E:\个人\test\5.24已检索.xlsx',sheet_name='数据汇总')
def func(s):
    t=s.value_counts().head(1)
    p,c=t.index[0],t.values[0]
    pct=c/s.shape[0]
    return p,pct
result=df.groupby('产品名称')['页面价'].apply(func).apply(pd.Series).reset_index()
result.columns=['产品名称','主流价格','占比']
result


**Output:**

image

过程:

image

image

image

方法二:

点击查看代码
import pandas as pd
import numpy as np
df=pd.read_excel(r'E:\个人\test\5.24已检索.xlsx',sheet_name='数据汇总')
a=df.groupby('产品名称')['页面价'].apply(lambda x:x.mode().iloc[0])
a.name='主流价格'
b=df.groupby('产品名称')['页面价'].apply(lambda x:x.value_counts().iloc[0]/x.shape[0])
b.name='占比'
result=pd.concat([a,b],axis=1).reset_index()
result


Output:
image

posted @ 2022-07-26 22:05  Jasmine_Lee  阅读(32)  评论(0)    收藏  举报