Reputation: 197
I am using the link (Python - calculating the range(highest - lowest) in different group) as guide for calculating different ranges for different groups. However, I am running into SyntaxError: invalid syntax error when I run my code, and I cannot figure out the reason.
Here is a sample dataset that I am using and the code that is giving me the error
Date Group Away Moneyline Away Pointspread Points
Sun Sep 1 A 10 4
Mon Sep 2 B NaN 6
Tue Sep 3 A 20 10
Wed Sep 4 C 39 4
range = df.groupby('Group').apply(lambda x: x."Away Pointspread points".max() - x."Away Pointspread points".min()
The output of the code is SyntaxError: invalid syntax. What I am expecting is the range for the observations in Group. Thanks in advance for your help.
Upvotes: 0
Views: 190
Reputation: 406
Try to use brackets when columns names have spaces, other special chars or use reserved words ('print', for example).
Also, the column name is Away Pointspread Points
(capital P in points).
Should look something like:
df.groupby('Group').apply(lambda x: x["Away Pointspread Points"].max() - x["Away Pointspread Points"].min())
Upvotes: 1
Reputation: 3219
This part: x."Away Pointspread points"
is incorrect. Try using x["Away Pointspread points"]
instead.
That would be:
range = df.groupby('Group').apply(lambda x: x["Away Pointspread points"].max() - x["Away Pointspread points"].min()
Upvotes: 1