Python Groupby—如何将逻辑检查结果应用于所有行
我有一套像这样的:Python Groupby—如何将逻辑检查结果应用于所有行,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一套像这样的: In [127]: df Out[127]: ID Date regular_entry 0 1 2014-01-31 12:13:14 True 1 2 2014-02-28 12:13:14 False 2 1 2014-03-31 12:13:14 True 3 1 2014-04-30 12:13:14 True 4 2 2014-05-31
In [127]: df
Out[127]:
ID Date regular_entry
0 1 2014-01-31 12:13:14 True
1 2 2014-02-28 12:13:14 False
2 1 2014-03-31 12:13:14 True
3 1 2014-04-30 12:13:14 True
4 2 2014-05-31 12:13:14 False
5 2 2014-06-30 12:13:14 True
6 3 2014-07-31 12:13:14 False
7 3 2014-08-31 12:13:14 True
8 3 2014-09-30 12:13:14 False
9 1 2014-10-31 12:13:14 True
我需要找到每个组是否有任何行,例如'regular\u entry'==False
(如果按'ID'
分组)
我正在使用和transform()
来实现这一点,如下所示,它非常有效:
In [134]: df['ever_irregular'] = df.groupby('ID')['regular_entry'].transform(lambda x: False if x.all() else True )
In [135]: df
Out[135]:
ID Date regular_entry ever_irregular
0 1 2014-01-31 12:13:14 True False
1 2 2014-02-28 12:13:14 False True
2 1 2014-03-31 12:13:14 True False
3 1 2014-04-30 12:13:14 True False
4 2 2014-05-31 12:13:14 False True
5 2 2014-06-30 12:13:14 True True
6 3 2014-07-31 12:13:14 False True
7 3 2014-08-31 12:13:14 True True
8 3 2014-09-30 12:13:14 False True
9 1 2014-10-31 12:13:14 True False
现在,我还需要找出每个组的最后一个条目(如果按'ID'
分组并考虑'Date'
的值)是否有'regular\u entry'==False
我知道我可以得到每个组的最后一个条目,如下所示:
In [138]: df.sort_values(by='Date').groupby('ID').nth(-1)['regular_entry']
Out[138]:
ID
1 True
2 True
3 False
Name: regular_entry, dtype: bool
In [152]: df_new = pd.DataFrame(latest_row_regular).rename(columns={'regular_entry':'latest_regular'})
In [155]: pd.merge(df, df_new, left_on='ID', right_index=True).sort_values(by='Date')
Out[155]:
ID Date regular_entry ever_irregular latest_regular
0 1 2014-01-31 12:13:14 True False True
1 2 2014-02-28 12:13:14 False True True
2 1 2014-03-31 12:13:14 True False True
3 1 2014-04-30 12:13:14 True False True
4 2 2014-05-31 12:13:14 False True True
5 2 2014-06-30 12:13:14 True True True
6 3 2014-07-31 12:13:14 False True False
7 3 2014-08-31 12:13:14 True True False
8 3 2014-09-30 12:13:14 False True False
9 1 2014-10-31 12:13:14 True False True
我现在已经明白我可以试着像这样加入这两个团队:
In [138]: df.sort_values(by='Date').groupby('ID').nth(-1)['regular_entry']
Out[138]:
ID
1 True
2 True
3 False
Name: regular_entry, dtype: bool
In [152]: df_new = pd.DataFrame(latest_row_regular).rename(columns={'regular_entry':'latest_regular'})
In [155]: pd.merge(df, df_new, left_on='ID', right_index=True).sort_values(by='Date')
Out[155]:
ID Date regular_entry ever_irregular latest_regular
0 1 2014-01-31 12:13:14 True False True
1 2 2014-02-28 12:13:14 False True True
2 1 2014-03-31 12:13:14 True False True
3 1 2014-04-30 12:13:14 True False True
4 2 2014-05-31 12:13:14 False True True
5 2 2014-06-30 12:13:14 True True True
6 3 2014-07-31 12:13:14 False True False
7 3 2014-08-31 12:13:14 True True False
8 3 2014-09-30 12:13:14 False True False
9 1 2014-10-31 12:13:14 True False True
这似乎工作得很好,然而,它确实似乎有很长的路要走。是否有更简单/更快的方法获取每个组的值(在groupby()
之后分组)并直接应用,而不是遵循所有中间步骤
谢谢你的帮助 您可以通过以下方式使用相同的
.transform
调用:
df['latest_regular'] = (df.groupby('ID')['regular_entry']
.transform(lambda x: x.iloc[-1]))
工作示例:
df['last_regular'] = df.groupby('ID')['regular_entry'].transform(lambda x: x.iloc[-1])
17:41:18 [26]: df
Out[26]:
ID regular_entry last_regular
0 1 True True
1 2 False True
2 1 True True
3 1 True True
4 2 False True
5 2 True True
6 3 False False
7 3 True False
8 3 False False
9 1 True True
我认为您可以使用:
我认为对于测试而言,最好使用lambda安装的print
instaed定制功能:
def f(x):
print x
print x.iloc[-1]
return x.iloc[-1]
df['latest_regular'] = df.groupby('ID')['regular_entry'].transform(f)
print df
测试后使用lambda函数。不幸的是,这不起作用,我得到
TypeError:last()缺少一个必需的位置参数:“offset”
,当我尝试使用offset
时,我得到NotImplementedError:“last”只支持DatetimeIndex
@Thanos,而不是[-1]
。last()?这也不起作用,我认为x
是这种情况下的一个元素,我认为x[-1]
在这里没有意义。在任何情况下,它都会给出KeyError
。感谢您的时间和努力,此解决方案非常有效。这似乎产生了预期的结果,谢谢!为什么x.iloc[-1]
获取了['regular\u entry']
系列中的最后一个元素,而x
不是系列中的一个元素?使用print()
我发现x
实际上是一个pd.series
,是['regular\u entry']
的子集,在这种情况下对每个组都有意义。谢谢你的帮助。