问题
How can I replace all cells in a boolean dataframe (True/False) with the index name of that cell, when "True"? For example:
df = pd.DataFrame(
[
[False, True],
[True, False],
],
index=["abc", "def"],
columns=list("ab")
)
comes out as:
df = pd.DataFrame(
[
[False, abc],
[def, False],
],
index=["abc", "def"],
columns=list("ab")
)
回答1:
Use df.mask:
Replace values where the condition is True.
df.mask(df,df.index)
a b
abc False abc
def def False
来源:https://stackoverflow.com/questions/60703065/replace-all-trues-in-a-boolean-dataframe-with-index-valule