第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何過濾將行保留在 Pandas 列中特定單詞列表之后的 DataFrame?

如何過濾將行保留在 Pandas 列中特定單詞列表之后的 DataFrame?

如何過濾在按日期排序的特定單詞列表之后保留行的數(shù)據(jù)框?我有一個(gè)看起來像的 df    Name    Date    Event   Col10   Sam 1/1/2020    Apple   Test11   Sam 1/2/2020    Apple   Test22   Sam 1/3/2020    BALL    Test13   Sam 1/3/2020    CAT Test24   Sam 1/5/2020    BALL    Test25   Sam 1/6/2020    Apple   Test36   Nick    1/5/2020    CAT Test37   Nick    1/6/2020    BALL    Test38   Nick    1/7/2020    Apple   Test39   Nick    1/8/2020    Apple   Test410  Cat 1/1/2020    Apple   Test111  Cat 1/2/2020    Bat Test2 df=pd.DataFrame({'Name': {0: 'Sam',  1: 'Sam',  2: 'Sam',  3: 'Sam',  4: 'Sam',  5: 'Sam',  6: 'Nick',  7: 'Nick',  8: 'Nick',  9: 'Nick',  10: 'Cat',  11: 'Cat '}, 'Date': {0: '1/1/2020',  1: '1/2/2020',  2: '1/3/2020',  3: '1/3/2020',  4: '1/5/2020',  5: '1/6/2020',  6: '1/5/2020',  7: '1/6/2020',  8: '1/7/2020',  9: '1/8/2020',  10: '1/1/2020',  11: '1/2/2020'}, 'Event': {0: 'Apple',  1: 'Apple',  2: 'BALL',  3: 'CAT',  4: 'BALL',  5: 'Apple',  6: 'CAT',  7: 'BALL',  8: 'Apple',  9: 'Apple',  10: 'Apple',  11: 'Bat'}, 'Col1': {0: 'Test1',  1: 'Test2',  2: 'Test1',  3: 'Test2',  4: 'Test2',  5: 'Test3',  6: 'Test3',  7: 'Test3',  8: 'Test3',  9: 'Test4',  10: 'Test1',  11: 'Test2'}})我想保留在我的活動(dòng)中發(fā)生 BALL 或 CAT 的最早日期之后的行。因此,在我的示例中,我需要消除第 1、2 行和第 11 行,因?yàn)槲覀儗?Apple 作為第一個(gè)事件。我嘗試使用event_filter = ['BALL','CAT']df = df.loc[df['Event'].isin(event_filter)]我還嘗試刪除基于事件的子集,但它也刪除了第 8 行。任何幫助,將不勝感激。我期待的結(jié)果是:    Name    Date    Event   Col10   Sam 1/3/2020    BALL    Test11   Sam 1/3/2020    CAT Test22   Sam 1/5/2020    BALL    Test23   Sam 1/6/2020    Apple   Test34   Nick    1/5/2020    CAT Test35   Nick    1/6/2020    BALL    Test36   Nick    1/7/2020    Apple   Test37   Nick    1/8/2020    Apple   Test48   Cat 1/2/2020    Bat Test2
查看完整描述

2 回答

?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

這樣的事情怎么樣?另外,好像有錯(cuò)別字。最后一行是 Bat,這應(yīng)該是 BALL 嗎?(根據(jù)您的預(yù)期輸出)


lst = ['CAT', 'BALL']

檢查事件中是否存在列表的選定元素。存在則賦1,不存在則賦0。


df['C'] = np.where(df['Event'].isin(lst), 1, 0)

在此之后,我們可以對(duì) C 列執(zhí)行 cumsum 并過濾行。這可以通過在 Name 上使用 groupby 并在 c 列上執(zhí)行 cumsum 并檢查是否存在大于 0 的 cumsum 來完成。只有當(dāng)該 groupby (Name) 的事件中存在列表的那些元素時(shí),才會(huì)發(fā)生大于 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


查看完整回答
反對(duì) 回復(fù) 2023-03-30
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

這有點(diǎn)難以理解(您是否將事件過濾器從 Bat 切換為 BALL?:D),而且您似乎正在嘗試讓每個(gè)人獲得第一個(gè)事件?


如果是這樣,我認(rèn)為您需要按名稱拆分?jǐn)?shù)據(jù)框,根據(jù)需要進(jìn)行過濾,然后重新組合。


這是第一次出現(xiàn)的小函數(shù):


def get_min_index(ser, event_filter):


    in_event = ser.isin(event_filter)

    return in_event.loc[in_event].index[0]

然后假設(shè)您的 df 已經(jīng)按照您的需要進(jìn)行了排序。


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)

也許有一個(gè)更優(yōu)雅的解決方案,但希望這就是您正在尋找的


查看完整回答
反對(duì) 回復(fù) 2023-03-30
  • 2 回答
  • 0 關(guān)注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)