【问题标题】:Replace the values of multiple rows with the values of another row based on a condition in Pandas根据 Pandas 中的条件将多行的值替换为另一行的值
【发布时间】:2023-03-08 14:38:01
【问题描述】:

我想将 A、B、C、D 列的值替换为 2011 年的唯一值 where region = '' 的值。例如,2011 年值为 1 的唯一列将将其 3、4、9、8 值替换为 6、6、6、6;然后将这种方法应用于唯一值 2 和 3。然后将删除 region = '' 的行。

与此相关的其他问题没有我正在寻找的答案。我尝试过使用 loc 但无济于事。

【问题讨论】:

    标签: python pandas dataframe data-manipulation


    【解决方案1】:

    我认为你需要这个:

    df=df.groupby(['unique','year']).agg('last').reset_index()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 2021-07-19
      • 1970-01-01
      • 2020-05-16
      • 2018-09-26
      • 1970-01-01
      • 2021-10-24
      • 2019-04-13
      相关资源
      最近更新 更多