【问题标题】:How to create a column for 'number of wickets'如何为“检票口数量”创建一列
【发布时间】:2021-12-09 19:39:49
【问题描述】:

它应该是什么样子的示例:

这是上表的输出:

{'batfast_id': {0: 'bfs1',
  1: 'bfs1',
  2: 'bfs1',
  3: 'bfs1',
  4: 'bfs1',
  5: 'bfs1',
  6: 'bfs1',
  7: 'bfs2',
  8: 'bfs2',
  9: 'bfs2',
  10: 'bfs2',
  11: 'bfs2',
  12: 'bfs2'},
 'score': {0: 1,
  1: 2,
  2: 0,
  3: 0,
  4: 1,
  5: 0,
  6: 3,
  7: 3,
  8: 0,
  9: 0,
  10: 2,
  11: 2,
  12: 0},
 'day_month_year': {0: '01-01-2020',
  1: '01-01-2020',
  2: '01-01-2020',
  3: '01-01-2020',
  4: '02-01-2020',
  5: '02-01-2020',
  6: '02-01-2020',
  7: '02-01-2020',
  8: '02-01-2020',
  9: '02-01-2020',
  10: '03-01-2020',
  11: '03-01-2020',
  12: '03-01-2020'},
 'runs': {0: 1,
  1: 3,
  2: 3,
  3: 3,
  4: 1,
  5: 1,
  6: 4,
  7: 3,
  8: 3,
  9: 3,
  10: 2,
  11: 4,
  12: 4},
 'deliveries_faced': {0: 1,
  1: 2,
  2: 3,
  3: 4,
  4: 1,
  5: 2,
  6: 3,
  7: 1,
  8: 2,
  9: 3,
  10: 1,
  11: 2,
  12: 3},
 'wicket': {0: 'Not Out',
  1: 'Not Out',
  2: 'Bowled',
  3: 'Caught',
  4: 'Not Out',
  5: 'Caught',
  6: 'Not Out',
  7: 'Not Out',
  8: 'Bowled',
  9: 'Bowled',
  10: 'Not Out',
  11: 'Not Out',
  12: 'Caught'},
 'no_of_wickets': {0: 0,
  1: 0,
  2: 1,
  3: 2,
  4: 0,
  5: 1,
  6: 1,
  7: 0,
  8: 1,
  9: 2,
  10: 0,
  11: 0,
  12: 1}} 

我正在尝试创建一个名为 no_of_wickets 的列,用于计算用户在打板球时失去了多少个三柱门。但是,当日期更改或batfast_id(用户)更改时,它必须重置为 0。

score 是玩家在该次投球中获得的跑动次数,runsscore 的累积次数。

runs 是使用以下代码创建的:df['runs']=df.groupby(['batfast_id','day_month_year'])['score'].cumsum()

deliveries_faced 使用此代码:df['deliveries_faced']=df.groupby(['batfast_id','day_month_year']).cumcount()+1

【问题讨论】:

    标签: python pandas pandas-groupby cumsum


    【解决方案1】:

    创建一个布尔掩码,如果wicket 不在,则设置为True,然后按batfast_idday_month_year 分组,最后计算累积总和。

    df['no_of_wickets'] = df.assign(is_out=df['wicket'].ne('Not Out')) \
                            .groupby(['batfast_id', 'day_month_year'])['is_out'] \
                            .cumsum()
    print(df)
    
    # Output:
       batfast_id  score day_month_year  runs  deliveries_faced   wicket  no_of_wickets
    0        bfs1      1     01-01-2020     1                 1  Not Out              0
    1        bfs1      2     01-01-2020     3                 2  Not Out              0
    2        bfs1      0     01-01-2020     3                 3   Bowled              1
    3        bfs1      0     01-01-2020     3                 4   Caught              2
    4        bfs1      1     02-01-2020     1                 1  Not Out              0
    5        bfs1      0     02-01-2020     1                 2   Caught              1
    6        bfs1      3     02-01-2020     4                 3  Not Out              1
    7        bfs2      3     02-01-2020     3                 1  Not Out              0
    8        bfs2      0     02-01-2020     3                 2   Bowled              1
    9        bfs2      0     02-01-2020     3                 3   Bowled              2
    10       bfs2      2     03-01-2020     2                 1  Not Out              0
    11       bfs2      2     03-01-2020     4                 2  Not Out              0
    12       bfs2      0     03-01-2020     4                 3   Caught              1
    

    【讨论】:

    • 是的,已经成功了,非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多