【问题标题】:Pandas: update the information of certain row [duplicate]熊猫:更新某些行的信息[重复]
【发布时间】:2018-08-08 23:30:06
【问题描述】:

我有一个数据框df,如下所示:

name     team
Peter     A
Mary      A
John      B

现在我想将 John 的团队从 B 更新到 C。我可以遍历数据框,找到带有 name = 'John' 的行,删除它并添加一个新行。但是,我想知道有没有更优雅的方法来做到这一点?谢谢!

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    使用loc:

    df.loc[df['name'] == 'John', 'team'] = 'C'
    

    结果输出:

        name team
    0  Peter    A
    1   Mary    A
    2   John    C
    

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2013-12-20
      • 2021-11-01
      • 2018-03-13
      • 1970-01-01
      • 2022-12-17
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多