我正在尝试添加一个新的计算字段。我正在尝试Adding calculated column(s) to a dataframe in pandas中的第二好的答案,因为它看起来很整洁,因此在我看来似乎是最好的。请随时提供更好的选择。
无论哪种方式,我的初始代码都在下面:
import pandas as pd
#https://github.com/sivabalanb/Data-Analysis-with-Pandas-and-Python/blob/master/nba.csv
dt_nba = pd.read_csv("data//nba.csv")
#note this is just basic function. I want to pass partitioned data like team's average salary
def GetSalaryIncrement(val):
return val * 1.1
dt_nba["SalaryPlus10Percent"] = map(GetSalaryIncrement,dt_nba["Salary"])
dt_nba[["Name","Team","Salary","SalaryPlus10Percent"]][:5]
但是,结果却不是我所期望的:
+----+---------------+----------------+--------------+--------------------------------+
| ID | Name | Team | Salary | SalaryPlus10Percent |
+----+---------------+----------------+--------------+--------------------------------+
| 0 | Avery Bradley | Boston Celtics | 7730337.0000 | <map object at 0x7fb819e9b7b8> |
| 1 | Jae Crowder | Boston Celtics | 6796117.0000 | <map object at 0x7fb819e9b7b8> |
| 2 | John Holland | Boston Celtics | nan | <map object at 0x7fb819e9b7b8> |
| 3 | R.J. Hunter | Boston Celtics | 1148640.0000 | <map object at 0x7fb819e9b7b8> |
| 4 | Jonas Jerebko | Boston Celtics | 5000000.0000 | <map object at 0x7fb819e9b7b8> |
+----+---------------+----------------+--------------+--------------------------------+
我特别希望传递“窗口/汇总数据”,以便在该窗口中适当地忽略Nan值。
在T-SQL中的示例我可以这样做:
-- INCREASE EACH PLAYERS SALARY BY 10% OF AVERAGE SALARY OF THE TEAM
SELECT NewSalary= Salary + (.1 * AVG(Salary) OVER (PARTITION BY Team))
FROM nba_data
如果可能的话,我想在熊猫那做。谢谢。
答案 0 :(得分:3)
我认为您正在寻找
dt_nba["Salary"]=dt_nba["Salary"].map(GetSalaryIncrement)
您也可以使用
GetSalaryIncrement(dt_nba["Salary"])
dt_nba["Salary"].apply(GetSalaryIncrement)
要计算INCREASE EACH PLAYERS SALARY BY 10% OF AVERAGE SALARY OF THE TEAM
dt_nba['Newsa']=dt_nba.groupby('Team')['Salary'].transform('mean')*0.1+dt_nba["Salary"]