[英]How to extract a subset of dataframe based on the last occurence of a string in a column in pandas?
I have a dataframe lik below我有一个 dataframe 喜欢下面
ID ID | name姓名 | number数字 |
---|---|---|
1 1 | Sta站 | 2 2 |
1 1 | Danny丹尼 | 5 5 |
1 1 | Sta站 | 2 2 |
1 1 | elle艾丽 | 4 4 |
1 1 | Sta站 | 2 2 |
1 1 | jake杰克 | 9 9 |
1 1 | Andy安迪 | 11 11 |
1 1 | Adam亚当 | 22 22 |
1 1 | blah废话 | 44 44 |
1 1 | blahblah废话 | 66 66 |
I want to extract the records till the last occurrence of Sta.我想提取记录,直到最后一次出现 Sta。 like this below像下面这样
ID ID | name姓名 | number数字 |
---|---|---|
1 1 | Sta站 | 2 2 |
1 1 | Danny丹尼 | 5 5 |
1 1 | Sta站 | 2 2 |
1 1 | elle艾丽 | 4 4 |
1 1 | Sta站 | 2 2 |
I am not sure how I can do that.我不确定我该怎么做。 Can someone please suggest?有人可以建议吗?
first, *_, last = df.index[df.name.eq('Sta')]
df.loc[first:last]
ID name number
0 1 Sta 2
1 1 Danny 5
2 1 Sta 2
3 1 elle 4
4 1 Sta 2
Get the first and last labels for Sta
and index df
获取Sta
和索引df
的第一个和最后一个标签
I would find Sta
's last occurrence and use it as the slicing parameter.我会找到Sta
的最后一次出现并将其用作切片参数。 There's an interesting function I didn't know that I found out working out this answer, last_valid_index()
might be really helpful here.有一个有趣的 function 我不知道我发现了这个答案, last_valid_index()
在这里可能真的很有帮助。
filtered = df.loc[:df[df['name']=='Sta'].last_valid_index(),:]
staRows = df.loc[df.name == "Sta", :]
lastStA = staRows.tail()
lastStaLoc = df.index[lastSta]
final = df.loc[0:lastStaLoc]
altFinal = df.loc[0:df.index[df.loc[df.name == "Sta", :].tail()]]
staRows is every row where name is Sta staRows 是名称为 Sta 的每一行
lastSta is the last row in staRows lastSta 是 staRows 中的最后一行
lastStaLoc is the location of lastSta lastStaLoc 是 lastSta 的位置
final should be rows up to lastStaLoc final 应该是直到 lastStaLoc 的行
altFinal is the one line solution if you want to be fancy.如果您想花哨的话,altFinal 是一种解决方案。 This is all untested so you may have to debug a bit:)这都是未经测试的,所以你可能需要调试一下:)
Let us do groupby
with transform
idxmax
让我们用transform
idxmax
做groupby
df[df.index<=df['name'].eq('Sta').iloc[::-1].groupby(df['ID']).transform('idxmax')]
ID name number
0 1 Sta 2
1 1 Danny 5
2 1 Sta 2
3 1 elle 4
4 1 Sta 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.