简体   繁体   中英

How to find the average of salary greater than the mean/median

How to find the average of salary greater than the mean/median. How to execute directly in pandas not by using pandasql

import pandas as pd
import pandasql as ps
df1 = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")  
df1.head(3) 
q5 = """SELECT Name,Salary FROM df1 WHERE Salary > (SELECT AVG(Salary) FROM df1) limit 2 """
print(ps.sqldf(q5, locals()))

My Out and Expected out

         Name     Salary
0  Avery Bradley  7730337.0
1    Jae Crowder  6796117.0

尝试这个:

 df1=df1[(df1.salary>np.mean(df1.salary))|(df1.salary>np.median(df1.salary)))

简单的单线,

df1[df1['Salary'] > df1['Salary'].mean()][0:2]

You query indicates you want only 2 columns

df1.loc[df1.Salary > df1.Salary.mean(), ['Name','Salary']]

Out[114]:
                 Name      Salary
0       Avery Bradley   7730337.0
1         Jae Crowder   6796117.0
4       Jonas Jerebko   5000000.0
5        Amir Johnson  12000000.0
11      Isaiah Thomas   6912869.0
..                ...         ...
433  Gerald Henderson   6000000.0
434       Chris Kaman   5016000.0
444        Alec Burks   9463484.0
446    Derrick Favors  12000000.0
448    Gordon Hayward  15409570.0

[150 rows x 2 columns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM