[英]Why when I use groupby on two columns the result is NaN but when I do it on one column it works correctly
I use the following dataframe我使用以下 dataframe
df = pd.DataFrame({'class': 'a a aa aa b b '.split(),
'item': [5,5,7,7,7,6],
'last_PO_code': ['103','103','103','104','103','104'],
'qty': [5,4,7,6,7,6]
})
and I apply this rules on it我在上面应用这个规则
regle1 = lambda x: True if x['last_PO_code'].all() == "103" else False
like this with one column on grouby像这样在grouby上有一栏
df['regle1'] = df['class'].map(df.groupby(['class']).apply(regle1))
I have the following result for me, it's good我有以下结果,很好
class item last_PO_code qty regle1
0 a 5 103 5 True
1 a 5 103 4 True
2 aa 7 103 7 False
3 aa 7 104 6 False
4 b 7 103 7 False
5 b 6 104 6 False
but when I want to groupby class and item, I have this result但是当我想按 class 和项目分组时,我有这个结果
df['regle1'] = df['class'].map(df.groupby(['class','item']).apply(regle1))
class item last_PO_code qty regle1
0 a 5 103 5 NaN
1 a 5 103 4 NaN
2 aa 7 103 7 NaN
3 aa 7 104 6 NaN
4 b 7 103 7 NaN
5 b 6 104 6 NaN
someone can help me to understand please??有人可以帮我理解吗??
it seems like you're looking for groupby+transform, but trying to implement that via pd.Series.map
.似乎您正在寻找 groupby+transform,但试图通过
pd.Series.map
来实现它。
df = pd.DataFrame({'class': 'a a aa aa b b '.split(),
'item': [5,5,7,7,7,6],
'last_PO_code': ['103','103','103','104','103','104'],
'qty': [5,4,7,6,7,6]
})
def regle1(x):
return (x == '103').all()
df['regle1'] = df.groupby(['class', 'item']).last_PO_code.transform(regle1)
The final dataframe now looks like this:最终的 dataframe 现在看起来像这样:
class item last_PO_code qty regle1
0 a 5 103 5 True
1 a 5 103 4 True
2 aa 7 103 7 False
3 aa 7 104 6 False
4 b 7 103 7 True
5 b 6 104 6 False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.