Replace value for a selected cell in pandas DataFrame without using index

前端 未结 3 1372
后悔当初
后悔当初 2020-11-29 18:44

this is a rather similar question to this question but with one key difference: I\'m selecting the data I want to change not by its index but by some criteria.

If th

3条回答
  •  自闭症患者
    2020-11-29 19:13

    Old question, but I'm surprised nobody mentioned numpy's .where() functionality (which can be called directly from the pandas module).

    In this case the code would be:

    d.sales = pd.np.where(d.sales == 24, 100, d.sales)
    

    To my knowledge, this is one of the fastest ways to conditionally change data across a series.

提交回复
热议问题