簡體   English   中英

如何獲得不包括小於或大於特定值的行的平均值並最后添加新列,Python,Pandas

[英]How to get an average of row excluding specific value less than or greater than and add new column at last, Python, Pandas

以下是我的輸入數據框

>>data frame after getting avg
   a  b  c  d  avg   
0  1  4  7  8  5  
1  3  4  5  6  4.5 
2  6  8  2  9  6.25
3  2  9  5  6  5.5   


Output required after adding criteria
>> 
   a  b  c  d  avg   avg_criteria
0  1  4  7  8  5     7.5 (<=5)
1  3  4  5  6  4.5   5.5 (<=4.5)
2  6  8  2  9  6.25  8.5 (<=6.25)
3  2  9  5  6  5.5   7.5 (<=5.5)

> This is the code I have tried

讀取文件

df_input_data = pd.DataFrame(pd.read_excel(file_path,header=2).dropna(axis=1, how= 'all'))

計算平均值后添加列

df_avg = df_input_data.assign(Avg=df_input_data.mean(axis=1, skipna=True))

標准

criteria = df_input_data.iloc[, :] >= df_avg.iloc[1][-1]

#創建output數據框

df_output = df_input_data.assign(Avg_criteria= criteria)


I am unable to solve this issue. I have tried and googled it many times

據我了解,您可以在與平均值比較后嘗試df.mask / df.where然后計算平均值:

m=df.drop("avg",1)
m.where(m.ge(df['avg'],axis=0)).mean(1)

0    7.5
1    5.5
2    8.5
3    7.5
dtype: float64

print(df.assign(Avg_criteria=m.where(m.ge(df['avg'],axis=0)).mean(1)))

   a  b  c  d   avg  Avg_criteria
0  1  4  7  8  5.00           7.5
1  3  4  5  6  4.50           5.5
2  6  8  2  9  6.25           8.5
3  2  9  5  6  5.50           7.5

暫無
暫無

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

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