[英]Select specific value in one column and get n rows before/after from another column in pandas
我有一个包含两列的数据框:trialType 和直径。 我需要找到trialType == 'start'的所有地方,然后获取这些位置前后所有2500行的直径,包括。 我尝试了以下方法:
idx = df.loc(df[df['trialType']=='start'])
df.iloc[idx - 2500 : idx + 2500]
我的目标是拥有一个只有那些相关行的数据框。
有趣的。
关于什么:
idx = df.loc[lambda x: x['trialType']=='start'].index
rows = df.loc[idx]
a = df.shift( 2500).loc[idx]
b = df.shift(-2500).loc[idx]
然后,您可以将它们组合在一起,但您认为最好。
pd.concat([a,rows,b])
你也可以这样做:
idx = df.loc[lambda x: x['trialType']=='start'].index
df.loc[lambda x: (x.index-2500).isin(idx)
¦ x.index.isin(idx)
¦ (x.index+2500).isin(idx)]
但是如果你的索引不是连续的(0、1、2、3等),你必须修改上面的代码
我只会将第一行更改为
idx = df.index[df['trialType']=="start"].tolist()[0]
这将返回条件为真的第一个索引
第 2 行应该可以正常工作df.iloc[idx - 2500 : idx + 2500]
您可以运行此代码来尝试
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'Z', 'C', 'C', 'D'],
'points': [5, 7, 7, 9, 12, 9, 9, 4],
'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
df
idx = df.index[df['team']=="B"].tolist()[0]
df.iloc[idx - 2 : idx + 2]
输出
team points rebounds
1 A 7 8
2 A 7 10
3 B 9 6
4 Z 12 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.