【问题标题】:How to filter certain words into a new column with pandas? [duplicate]如何使用熊猫将某些单词过滤到新列中? [复制]
【发布时间】:2020-06-19 03:49:37
【问题描述】:

我有一个Status 列,如下所示:

      Status
      ------------------------
4     probable for Thursday
7     plans to play on Tuesday
8     questionable Tuesday
13    won't play on Sunday
15    will start on Saturday
16    is questionable
17    will not play Thursday
32    questionable Monday
35    good to go vs. CLE
36    probable vs. Cavaliers
37    questionable Monday
40    will not play Saturday
41    drops 35/16/7 on Mavs
42    will play vs. DAL
43    probable vs. Mavericks
45    will play vs. Knicks
47    'hopeful' to play Tues
52    will play on Sunday
55    will play on Saturday
56    headed toward a GTD?

我想创建一个名为Game_Status 的新列。 Game_Status 将过滤 Status 列,如下所示:

      Status                      Game_Status
      ------------------------    ------------
4     probable for Thursday       probable
7     plans to play on Tuesday    plans to play
8     questionable Tuesday        questionable
13    won't play on Sunday        won't play
15    will start on Saturday      will start
16    is questionable             questionable
17    will not play Thursday      will not play
32    questionable Monday         questionable
35    good to go vs. CLE          good to go
36    probable vs. Cavaliers      probable
37    questionable Monday         questionable
40    will not play Saturday      will not play
41    drops 35/16/7 on Mavs       
42    will play vs. DAL           will play
43    probable vs. Mavericks      probable
45    will play vs. Knicks        will play
47    'hopeful' to play Tues      hopeful
52    will play on Sunday         will play
55    will play on Saturday       will play
56    headed toward a GTD?        GTD

在第 41 行,Game_Status 将留空,因为找不到任何单词/短语。过滤器将通过这些单词/短语:

gamestatuswords = ['out', 'questionable', 'doubtful','locker room','won\'t return','won\'t play','fractured','sprained','hyperextended','bruised',
                    'probable to return', 'uncertain', 'game-time decision','miss','weeks', 'GTD','suspended','suspension','day-to-day',
                    'game time decision', 'broken', 'torn', 'separated', 'ACL','unlikely to play','will not play','without timetable','retire',
                    'ejected','ejection','probable','hopeful','will play', 'available to play','will start','plans to play','good to go','cleared']

如何实现gamestatuswords 以通过Status 列过滤获得Game_Status 列?

我已经试过了:

df['Game_Status'] = np.where(df['Status'].eq('gamestatuswords'))

【问题讨论】:

    标签: python pandas numpy dataframe


    【解决方案1】:

    你可以使用这个正则表达式模式:

    df['Game-status'] = (df.Status
                           .str.extract(fr'\b({"|".join(gamestatuswords)})\b')[0]
                           .fillna('')
                        )
    

    输出:

                          Status    Game-status
    4      probable for Thursday       probable
    7   plans to play on Tuesday  plans to play
    8       questionable Tuesday   questionable
    13      won't play on Sunday     won't play
    15    will start on Saturday     will start
    16           is questionable   questionable
    17    will not play Thursday  will not play
    32       questionable Monday   questionable
    35        good to go vs. CLE     good to go
    36    probable vs. Cavaliers       probable
    37       questionable Monday   questionable
    40    will not play Saturday  will not play
    41     drops 35/16/7 on Mavs               
    42         will play vs. DAL      will play
    43    probable vs. Mavericks       probable
    45      will play vs. Knicks      will play
    47    'hopeful' to play Tues        hopeful
    52       will play on Sunday      will play
    55     will play on Saturday      will play
    56      headed toward a GTD?            GTD
    

    【讨论】:

      【解决方案2】:

      Series.str.findall 与正则表达式模式一起使用:

      pattern = '|'.join(gamestatuswords)
      df['Game_Status'] = df['Status'].str.findall(pattern).str.join(', ')
      

      # print(df)
                            Status    Game_Status
      4      probable for Thursday       probable
      7   plans to play on Tuesday  plans to play
      8       questionable Tuesday   questionable
      13      won't play on Sunday     won't play
      15    will start on Saturday     will start
      16           is questionable   questionable
      17    will not play Thursday  will not play
      32       questionable Monday   questionable
      35        good to go vs. CLE     good to go
      36    probable vs. Cavaliers       probable
      37       questionable Monday   questionable
      40    will not play Saturday  will not play
      41     drops 35/16/7 on Mavs               
      42         will play vs. DAL      will play
      43    probable vs. Mavericks       probable
      45      will play vs. Knicks      will play
      47    'hopeful' to play Tues        hopeful
      52       will play on Sunday      will play
      55     will play on Saturday      will play
      56      headed toward a GTD?            GTD
      

      【讨论】:

      • 如何加载Series.str.findall
      • 你不必加载它,它只是熊猫系列的属性。
      猜你喜欢
      • 2020-07-24
      • 2021-01-10
      • 2016-08-02
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2019-04-04
      • 2020-11-12
      • 1970-01-01
      相关资源
      最近更新 更多