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

Python 从Panda中的列中获取位于前n%的值的百分比,如25%、50%等或低于n%,python,python-3.x,pandas,django-models,django-rest-framework,Python,Python 3.x,Pandas,Django Models,Django Rest Framework,我有一个这样的数据框- 我希望表中有这样的列- 最后一张表是这样的- 如何计算这些列 我有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(['

我有一个这样的数据框-

我希望表中有这样的列-

最后一张表是这样的-

如何计算这些列

我有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)
这段代码以数据帧的形式给我输出-

金额=项目按金额分摊的百分比

数量=按数量划分的项目贡献百分比

请帮我找出正确答案。

您正在寻找答案和一些基础数学

在表中显示这些值没有多大价值-它的3个以上的列乘以
len(df)
数据都是一样的-因此我将它们作为简单的语句给出:

import pandas as pd
import random

# some data shuffling to see it works on unsorted data
random.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']) 

# calculate the quantile series
q25 = 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=" ") 
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")
输出:

price    247.5
Name: 0.25, dtype: float64

price    495.0
Name: 0.5, dtype: float64

price    742.5
Name: 0.75, dtype: float64

Bottom 25% of prices are below/equal to 247.5 thats 25.0% of all items
Bottom 50% of prices are below/equal to 495.0 thats 50.0% of all items
Bottom 75% of prices are below/equal to 742.5 thats 75.0% of all items
price    247.5
Name: 0.25, dtype: float64

price    495.0
Name: 0.5, dtype: float64

price    742.5
Name: 0.75, dtype: float64

Bottom 25% of prices are below/equal to 247.5 thats 25.0% of all items
Bottom 50% of prices are below/equal to 495.0 thats 50.0% of all items
Bottom 75% of prices are below/equal to 742.5 thats 75.0% of all items