2 回答

TA贡献1982条经验 获得超2个赞
这样的事情怎么样?另外,好像有错别字。最后一行是 Bat,这应该是 BALL 吗?(根据您的预期输出)
lst = ['CAT', 'BALL']
检查事件中是否存在列表的选定元素。存在则赋1,不存在则赋0。
df['C'] = np.where(df['Event'].isin(lst), 1, 0)
在此之后,我们可以对 C 列执行 cumsum 并过滤行。这可以通过在 Name 上使用 groupby 并在 c 列上执行 cumsum 并检查是否存在大于 0 的 cumsum 来完成。只有当该 groupby (Name) 的事件中存在列表的那些元素时,才会发生大于 0 的情况
df = df.loc[df.groupby('Name')['C'].cumsum()>0].reset_index(drop=True)
df.drop('C', 1, inplace=True)
print (df)
Name Date Event Col1
0 Sam 1/3/2020 BALL Test1
1 Sam 1/3/2020 CAT Test2
2 Sam 1/5/2020 BALL Test2
3 Sam 1/6/2020 Apple Test3
4 Nick 1/5/2020 CAT Test3
5 Nick 1/6/2020 BALL Test3
6 Nick 1/7/2020 Apple Test3
7 Nick 1/8/2020 Apple Test4

TA贡献1848条经验 获得超6个赞
这有点难以理解(您是否将事件过滤器从 Bat 切换为 BALL?:D),而且您似乎正在尝试让每个人获得第一个事件?
如果是这样,我认为您需要按名称拆分数据框,根据需要进行过滤,然后重新组合。
这是第一次出现的小函数:
def get_min_index(ser, event_filter):
in_event = ser.isin(event_filter)
return in_event.loc[in_event].index[0]
然后假设您的 df 已经按照您的需要进行了排序。
tdf_lst = []
names = df['Name'].unique()
for name in names:
tdf = df.loc[df['Name']==name, :] # filter for the individual name
min_idx = get_min_index(tdf['Event'], event_filter) # get the first index
tdf = tdf.loc[min_idx:,:] # select from the first index to the last
tdf_lst.append(tdf)
df_fltrd = pd.concat(tdf_lst)
也许有一个更优雅的解决方案,但希望这就是您正在寻找的
添加回答
举报