Python Lambda函数语法来转换Pandas Groupby数据框 [英] Python lambda function syntax to transform a pandas groupby dataframe

查看:97
本文介绍了Python Lambda函数语法来转换Pandas Groupby数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个非常简单的问题.我有两行代码.第一个作品.第二个错误如下:

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

这是两行代码.第一行(工作正常)对off0_on1 == 1的行进行计数.第二行尝试对off0_on1 == 0的行进行计数.

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)

这是熊猫数据框:

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?

修改:使用映射函数建议了两个答案,该函数将产生以下输出."on1"列对我而言是正确的;"off0"列不正确.对于第一个"del_month","off0"列的结果应与"on1"列的结果相同.对于第二个"del_month","off0"列应全部为1(即1、1、1、1).

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))

编辑2 不知道这是否可以澄清事情,但是最终我希望熊猫能够轻松地执行以下SQL代码:

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

编辑3 新问题包含我需要的答案.谢谢大家!

Edit 3 This new question contains the answer I need. Thanks everyone!

推荐答案

先前的答案通过在结尾处添加"else 0",迅速解决了我的lambda函数错误.我的最终问题是

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)) 

这篇关于Python Lambda函数语法来转换Pandas Groupby数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆