簡體   English   中英

Pandas:如何從其他列的過濾值中獲取該列的平均值?

[英]Pandas : How to get mean of the column from the filtered values of other column?

 Name      Runs   Score         
 Richard   2000    2
 Richard   3000    3
 Richard   7000    8
 Richard   8000    7
 Richard   2000    4
 Richard   9000    7.5

lessthan1 = str(len(df[(df['Score']>=0) & (df['Score']<1)]))
oneto5 = str(len(df[(df['Score']>=1) & (df['Score']<5)]))
fiveto10 = str(len(df[(df['Score']>=5) & (df['Score']<10)]))

例如在類別 >=1 到 <5..am 期望 (2000+3000+2000)/31000

看起來您可以使用pd.cutpd.cut得分數據並使用它來對運行進行分組,例如:

df.groupby(pd.cut(df['Score'], [0, 1, 5, 10]))['Runs'].mean()

給你:

Score
(0, 1]             NaN
(1, 5]     2333.333333
(5, 10]    8000.000000
Name: Runs, dtype: float64

您可以嘗試使用布爾掩碼如下,

lessthan1 = df.loc[(df['Score'] >= 0) & (df['Score'] <1), 'Runs'].mean()
oneto5 = df.loc[(df['Score']>=1) & (df['Score']<5), 'Runs'].mean()
fiveto10 = df.loc[(df['Score']>=5) & (df['Score']<10), 'Runs'].mean()

print('Less than 1:', lessthan1)
print('One to Five:', oneto5)
print('Five to Ten:', fiveto10)
import pandas as pd
array = [
    ["Richard",   2000,    2],
    ["Richard",   3000,    3],
    ["Richard",   7000,    8],
    ["Richard",   8000,    7],
    ["Richard",   2000,    4],
    ["Richard",   9000,    7.5]]
df = pd.DataFrame(array, columns=["Name", "Runs", "Score"])
print(df[(df['Score'] >= 0) & (df['Score'] < 1)]["Runs"].mean())
print(df[(df['Score'] >= 1) & (df['Score'] < 5)]["Runs"].mean())
print(df[(df['Score'] >= 5) & (df['Score'] < 10)]["Runs"].mean())

2333.33333333333335

8000.0

您只需要選擇 Runs 列,然后在實現布爾選擇后對其使用 .mean() 方法

此外,您可以使用帶有 lambdas 的 pandas 應用函數:

df.loc[df['Score'].apply(lambda x: x < 1 and x > 0), 'Runs'].mean()
df.loc[df['Score'].apply(lambda x: x >= 1 and x < 5), 'Runs'].mean()
df.loc[df['Score'].apply(lambda x: x >= 5 and x < 10), 'Runs'].mean()

結果:

5166.666666666667
nan
2333.3333333333335

要獲得分數總和的平均值,您必須將mean()更改為sum()並除以總sum() 例如

df.loc[df['Score'].apply(lambda x: x >= 1 and x < 5), 'Runs'].sum()/df['Runs'].sum()

結果:

0.22580645161290322 = (2000 + 3000 + 2000)/31000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM