如何找到大于平均值/中位数的平均工资。如何使用 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()))
我的外出和期待出局
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)))
Simple Oneliner,
df1[df1['Salary'] > df1['Salary'].mean()][0:2]
查询表明您只需要 2 列
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]