问题
This should be a very simple question to answer. I have two lines of code. The first one works. The second gives the following error:
SyntaxError: invalid syntax
Here are the two lines of code. The first line (which works fine) counts the rows where off0_on1 == 1. The second one trys to count the rows where off0_on1 == 0.
a1['on1'] = a1.groupby('del_month')['off0_on1'].transform(sum)
a1['off0'] = a1.groupby('del_month')['off0_on1'].transform(lambda x: 1 if x == 0)
Here is the pandas dataframe:
a1 = pd.DataFrame({'del_month':[1,1,1,1,2,2,2,2], 'off0_on1':[0,0,1,1,0,1,1,1]})
Any suggestions to revise the second line of code above?
Edit: Two of the answers have suggested using a map function, which produces the following output. The "on1" column is correct for my purposes; the "off0" column is not correct. For the first "del_month", the "off0" column should have the same results as the "on1" column. For the second "del_month", the "off0" column should be all ones (i.e. 1, 1, 1, 1).
Here's what happens when I use the following map function (see image below):
a1['off0'] = a1.groupby('del_month')['off0_on1'].transform(lambda series: map(lambda x: 1 if x == 0 else 0, series))
Edit 2 Not sure if this clarifies things, but ultimately I want pandas to do what the following SQL code does so easily:
select
del_month
, sum(case when off0_on1 = 1 then 1 else 0 end) as on1
, sum(case when off0_on1 = 0 then 1 else 0 end) as off0
from a1
group by del_month
order by del_month
Edit 3 This new question contains the answer I need. Thanks everyone!
回答1:
When you define the if
statement (shorthand way) you need to define the else
as well.
lambda x: 1 if x == 0 else 0 # For example.
EDIT:
In your first lambda it's a Series Actually, so you need to loop it (with map for example)
a1['off0'] = a1.groupby('del_month')['off0_on1'].transform(lambda x: map(lambda value: 1 if value == 0 else 0, x))
回答2:
Your code: lambda x: 1 if x == 0
is not a valid expression. Try something like lambda x: 1 if x == 0 else 0
See below:
>>> example = lambda x: 1 if x == 0 else 0
>>> example(0)
1
>>> example(1)
0
回答3:
The previous answers quickly fixed my lambda function error by adding "else 0" at the end. My ultimate question was answered here with the following line of code:
a1['off0'] = a1.groupby('del_month')['off0_on1'].transform(lambda x: sum(x==0))
来源:https://stackoverflow.com/questions/53105016/python-lambda-function-syntax-to-transform-a-pandas-groupby-dataframe