亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

小怪獸愛吃肉 2022-10-06 17:03:02
我有一個這樣的數據框 -我想要表格中這樣的列-所以決賽桌是這樣的——我如何計算這些列。我在 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)這段代碼以數據框的形式給了我輸出 -amount_per = 按金額計算的項目貢獻百分比quantity_per = 按數量計算的項目貢獻百分比請幫我找出正確的答案。
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

您正在尋找df.quantile和一些基本數學。

在表中顯示這些值并沒有多大價值——它的 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")

(未洗牌)數據框看起來像


           name  price

0   product   1      0

1   product   2     10

2   product   3     20 

..          ...    ...  

97  product  98    970

98  product  99    980

99  product 100    990


[100 rows x 2 columns]

輸出:


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


查看完整回答
反對 回復 2022-10-06
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號