【问题标题】:How to change column values based on value of an index如何根据索引值更改列值
【发布时间】:2022-08-09 21:58:25
【问题描述】:
                                                                S/R
Batfast_id   Session_no   Event_Name   Overs  Deliveries_faced
bfs_1        1            fulham       0      5                  20
                                       1      11                 45
                                       2      17                 30
bfs_2        1            gps          0      5                  55
                                       1      11                 34
                                       2      17                 27

我有一个如上所述的 df,其多重索引为Batfast_id, Session_no, Event_Name, Overs, Deliveries_faced。我想让S/R = 0,Deliveries_faced 的任何地方都是 5。

    标签: pandas dataframe multi-index


    【解决方案1】:

    尝试这个:

    import numpy as np
    df['S/R'] = np.where(df['Deliveries_faced']==5, 0, df['S/R'])
    

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2020-06-30
      相关资源
      最近更新 更多