简体   繁体   English

为什么当我在两列上使用 groupby 时结果是 NaN 但是当我在一列上使用它时它可以正常工作

[英]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.

相关问题 为什么当我在程序中使用两个功能时,只有一个可以工作 - Why when I use two functions in a program only one works 为什么我的数据框会在我进行分组后丢弃一个列?(正在丢弃的列是我用来分组的列之一) - Why is my dataframe dropping a coulmn once i do a groupby?(The column that is dropping is one of the columns that im using to group) 使用groupby时如何在一个熊猫中用一个函数聚合多列? - How do I aggregate multiple columns with one function in pandas when using groupby? 如何使用 groupby 而不在 NaN 列中创建其他值? - How do I use groupby without making other values in the column NaN? 当我使用 DataFrame 输出列时,为什么我的数据输出中的值是 NaN? - Why the values from my data output as NaN when I use DataFrame for outputting a column? 当我加入两个其中没有NaN的DataFrame(多级索引)时,为什么会得到NaN? - Why am I getting NaN when I join two DataFrame that have no NaN in them (multilevel index)? 当其中一列是 dataframe 中的索引时,如何合并多列上的两个数据框 - How do I merge two dataframes on multiple columns when one of the columns is an index in one dataframe 如何获取作为 groupby 方法结果的两列的除值 - How do I get the divided values of two columns that are a result from a groupby method 为什么我在编码时总是得到一个 nan 集? - Why do I keep getting a nan set when coding? 为什么在使用.mean()时得到NaN - Why do I get NaN when using .mean()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM