【问题标题】:Replace all Trues in a boolean dataframe with index valule用索引值替换布尔数据框中的所有轮胎
【发布时间】:2020-06-27 09:27:19
【问题描述】:

当“True”时,如何将布尔数据框(True/False)中的所有单元格替换为该单元格的索引名称?例如:

df = pd.DataFrame(
    [
        [False, True],
        [True, False],

    ],
    index=["abc", "def"],
    columns=list("ab")
)

结果如下:

df = pd.DataFrame(
    [
        [False, abc],
        [def, False],

    ],
    index=["abc", "def"],
    columns=list("ab")
)

【问题讨论】:

    标签: python-3.x pandas replace


    【解决方案1】:

    使用df.mask:

    替换条件为 True 的值。

    df.mask(df,df.index)
    

             a      b
    abc  False    abc
    def    def  False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 2015-04-08
      • 2012-03-22
      • 2012-08-18
      • 2012-10-19
      相关资源
      最近更新 更多