从 Panda 中的列中获取位于前 n% 的值的百分比,例如 25%、50% 等或低于 n%

我有一个这样的数据框 -

http://img2.mukewang.com/633e99dc0001f7b903430223.jpg

我想要表格中这样的列-

http://img1.mukewang.com/633e99e40001a9d303050226.jpg

所以决赛桌是这样的——


http://img4.mukewang.com/633e99f20001c62d06500221.jpg

我如何计算这些列。


我在 django rest API 中有当前代码-


@api_view(['GET','POST'])

def sale_prod(request):       

if request.method == 'GET':


    data = sales_products.objects.values()

    df = pd.DataFrame(data)

    df = df.groupby(['item_id','item_code'])['amount','quantity'].sum().reset_index()

    df.dropna(inplace=True)

    df['amount_per'] = (df.amount / df.amount.sum())*100          # revenue contribution

    df['quantity_per'] = (df.quantity / df.quantity.sum())*100    # unit sold contribution

    df = df.round({'quantity': 0, 'amount':2, 'amount_per':2, 'quantity_per':2})


    main_list = []

    for ind in df.index:


        dict1 = {}

        dict1['item_code'] = df['item_code'][ind]

        dict1['amount'] = df['amount'][ind]

        dict1['quantity'] = df['quantity'][ind]

        dict1['amount_per'] = df['amount_per'][ind]

        dict1['quantity_per'] = df['quantity_per'][ind]

        main_list.append(dict1)


    return Response(main_list)

这段代码以数据框的形式给了我输出 -

http://img1.mukewang.com/633e9a0700011cc706450325.jpg

amount_per = 按金额计算的项目贡献百分比

quantity_per = 按数量计算的项目贡献百分比

请帮我找出正确的答案。


小怪兽爱吃肉
浏览 175回答 1
1回答

神不在的星期二

您正在寻找df.quantile和一些基本数学。在表中显示这些值并没有多大价值——它的 3 列以上乘以len(df)数据都是一样的——所以我将它们作为简单的语句给出:import pandas as pdimport random# some data shuffling to see it works on unsorted datarandom.seed(42)data = [[f"product {i+1:3d}",i*10] for i in range(100)]random.shuffle(data)df = pd.DataFrame(data, columns=['name', 'price'])&nbsp;# calculate the quantile seriesq25 = df.quantile(.25, numeric_only=True)q50 = df.quantile(.5, numeric_only=True)q75 = df.quantile(.75, numeric_only=True)print (q25, q50, q75, sep="\n\n")print( f"Bottom 25% of prices are below/equal to {q25.price} thats", end=" ")&nbsp;print( f"{len(df[df.price <= q25.price]) / (len(df) / 100)}% of all items")print( f"Bottom 50% of prices are below/equal to {q50.price} thats", end=" ")print( f"{len(df[df.price <= q50.price]) / (len(df) / 100)}% of all items")print( f"Bottom 75% of prices are below/equal to {q75.price} thats", end= " ")print( f"{len(df[df.price <= q75.price]) / (len(df)/ 100)}% of all items")(未洗牌)数据框看起来像&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name&nbsp; price0&nbsp; &nbsp;product&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; 01&nbsp; &nbsp;product&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;102&nbsp; &nbsp;product&nbsp; &nbsp;3&nbsp; &nbsp; &nbsp;20&nbsp;..&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; ...&nbsp;&nbsp;97&nbsp; product&nbsp; 98&nbsp; &nbsp; 97098&nbsp; product&nbsp; 99&nbsp; &nbsp; 98099&nbsp; product 100&nbsp; &nbsp; 990[100 rows x 2 columns]输出:price&nbsp; &nbsp; 247.5Name: 0.25, dtype: float64price&nbsp; &nbsp; 495.0Name: 0.5, dtype: float64price&nbsp; &nbsp; 742.5Name: 0.75, dtype: float64Bottom 25% of prices are below/equal to 247.5 thats 25.0% of all itemsBottom 50% of prices are below/equal to 495.0 thats 50.0% of all itemsBottom 75% of prices are below/equal to 742.5 thats 75.0% of all items
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python