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