I am trying to add a new calculated field. I am trying the 2nd best answer in Adding calculated column(s) to a dataframe in pandas because it seems the best in my opinion as it is neat. Please feel free to offer better alternatives.
Either way my initial code is below:
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]
However, the result is not what I expected:
+----+---------------+----------------+--------------+--------------------------------+
| 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> |
+----+---------------+----------------+--------------+--------------------------------+
In particular I am interested in passing "window/aggregate data" where it should gracefully ignore Nan values.
Example in T-SQL I can do this:
-- 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
I want to do that in Pandas if possible. Thank you.