简体   繁体   English

Pandas:如何在 lambda 公式中使用 (df.groupby)

[英]Pandas: How to use (df.groupby) in a lambda formula

The example below:下面的例子:

import pandas as pd
list1 = ['a','a','a','b','b','b','b','c','c','c']
list2 = range(len(list1))
df = pd.DataFrame(zip(list1, list2), columns=  ['Item','Value'])
df

gives:给出:

在此处输入图像描述

required: GroupFirstValue column as shown below.必需:GroupFirstValue 列,如下所示。

在此处输入图像描述

The idea is to use a lambda formula to get the 'first' value for each group..for example "a"'s first value is 0, "b"'s first value is 3, "c"'s first value is 7. That's why those numbers appear in the GroupFirstValue column.这个想法是使用 lambda 公式来获得每个组的“第一个”值。例如“a”的第一个值为 0,“b”的第一个值为 3,“c”的第一个值为7. 这就是为什么这些数字出现在 GroupFirstValue 列中的原因。

Note: I know that I can do this on 2 steps...one is the original df and the second is a grouped by df and then merge them together.注意:我知道我可以通过 2 个步骤执行此操作……一个是原始 df,第二个是按 df 分组,然后将它们合并在一起。 The idea is to see if this can be done more efficiently in a single step.我们的想法是看看这是否可以在一个步骤中更有效地完成。 Many thanks in advance!提前谢谢了!

groupby and use first groupby 并首先使用

df.groupby('Item')['Value'].first()

or you can use transform and assign to a new column in your frame或者您可以使用转换并分配给框架中的新列

df['new_col'] = df.groupby('Item')['Value'].transform('first')

Use mask and duplicated使用maskduplicated

df['GroupFirstValue'] = df.Value.mask(df.Item.duplicated())

Out[109]:
  Item  Value  GroupFirstValue
0    a      0              0.0
1    a      1              NaN
2    a      2              NaN
3    b      3              3.0
4    b      4              NaN
5    b      5              NaN
6    b      6              NaN
7    c      7              7.0
8    c      8              NaN
9    c      9              NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 pandas 中的 df.groupby() 上使用 apply() - how to use apply() on df.groupby() in pandas Pandas:如何在函数内将 sum() 或 mean() 分配给 df.groupby? - Pandas: How to assign sum() or mean() to df.groupby inside a function? 如何从 pandas 中的 df.groupby 更改 output 的索引名称 - How to change index names of the output from df.groupby in pandas Python:在 pandas 中成功组合 str.contains 和 df.groupby - Python: combining str.contains and df.groupby successfully in pandas 与df.groupby(...)相比,df.groupby(...).agg(set)产生不同的结果.agg(lambda x:set(x)) - df.groupby(…).agg(set) produces different result compared to df.groupby(…).agg(lambda x: set(x)) 通过df.groupby()将函数应用于熊猫DataFrame-造成麻烦 - Applying function, via df.groupby(), to pandas DataFrame - causing difficulties python pandas,DF.groupby()。agg(),agg()中的列引用 - python pandas, DF.groupby().agg(), column reference in agg() Pandas:根据组聚合过滤 DataFrameGroupBy (df.groupby) - Pandas: Filter DataFrameGroupBy (df.groupby) based on group aggregates df.groupby()的一些操作 - A few operations with df.groupby() df.groupby('A')。agg('min')如何转换为功能工具? - How does df.groupby('A').agg('min') translate to featuretools?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM