简体   繁体   English

Pandas:根据不同组中另一列的值过滤行(合计两列)

[英]Pandas: filter the row according to the value of another column in different group (two columns in aggregate)

I have a dataset like below in pandas dataframe:我在熊猫数据框中有一个如下所示的数据集:

Name    Shift   Data Type
Peter   0       12    A   
Peter   0       13    A
Peter   0       14    B
Sam     1       12    A
Sam     1       15    A
Sam     1       16    B
Sam     1       17    B
Mary    2       20    A
Mary    2       21    A
Mary    2       12    A

May anyone suggest how to show end result like the below?有人可以建议如何显示如下最终结果吗? (logic is: if shift is 0, pick the 1st item under groupby "Name" and "type" columns; if shift is 1, pick the 2nd value under the groupby "Name" and "type" columns, etc... I have thought of nth(x) but I don't know how to put a variable on x in this case. Other workaround is fine that can generated the same result. Thank you. (逻辑是:如果shift为0,选择groupby“Name”和“type”列下的第一个项目;如果shift为1,选择groupby“Name”和“type”列下的第二个值,等等......我已经想到了 nth(x) 但我不知道在这种情况下如何在 x 上放置变量。其他解决方法很好,可以生成相同的结果。谢谢。

Name    Shift   Data   Type
Peter   0       12     A
Peter   0       14     B
Sam     1       15     A
Sam     1       17     B
Mary    2       12     A

You can use groupby.cumcount()您可以使用groupby.cumcount()

Assuming your data is in a DataFrame called df , I think this should work for you:假设您的数据位于名为df的 DataFrame 中,我认为这应该对您有用:

df = df[df.groupby(['Name','Type']).cumcount()==df['Shift']]

It compares the cumulative count of rows with the same Name and Type to the values in the Shift column to determine which rows should be kept它将具有相同名称和类型的行的累积计数与 Shift 列中的值进行比较,以确定应保留哪些行

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

相关问题 使用组过滤器,当列值在另一个行列值的范围内时,熊猫选择行 - Pandas select rows when column value within range from another row column value with group filter 根据另外两列的值填写一个pandas列 - Fill a pandas column according to the value of two other columns 如何 select pandas 行在一个列中具有最大值,来自一组共享两个公共列的行? - How to select pandas row with maximum value in one column, from a group of rows that share two common columns? 按Group By Pandas创建两个聚合列 - Create two aggregate columns by Group By Pandas Pandas 按两列分组,并按每组计算第二列值 - Pandas group by two columns and count the second column value by each group 根据另一个列值重新采样和聚合数据 - Resample and aggregate data according to another column value Pandas:基于另一列的过滤器聚合 - Pandas: aggregate based on filter on another column 熊猫数据框:按两列分组,然后对另一列取平均值 - Pandas dataframe: Group by two columns and then average over another column 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 熊猫分组,汇总两列并返回一列的最早开始日期 - Pandas group, aggregate two columns and return the earliest Start Date for one column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM