繁体   English   中英

获取DataFrame中特定单元格的行列名

[英]Get Row and Column name of a specific cell in DataFrame

我有这个数据框,它代表我拥有的另一个数据框的相关矩阵。 我想遍历相关矩阵并获取单元格值为 True 的每一行和每一列的名称。 例如,我希望它打印:

雷布德雷布。 . .

在此处输入图像描述

reqd_Index = df[df['id']== True].index.tolist()
print(reqd_Index)

假设您有以下 dataframe:

df = pd.DataFrame({'AST': {'id': False, 'REB': False, 'FG3a': False},
 'BLK': {'id': False, 'REB': False, 'FG3a': False},
 'DREB': {'id': False, 'REB': True, 'FG3a': False}})

        AST    BLK   DREB
id    False  False  False
REB   False  False   True
FG3a  False  False  False
  1. 您可以melt DataFrame 并返回您感兴趣的行

true = df.melt(ignore_index=False)
true[true['value']]

        variable  value
REB     DREB      True
  1. 如果你想print ,你可以这样做:

true = df.melt(ignore_index=False)
true = true[true['value']]
[print(x,y) for (x,y) in zip(true.index, true['variable'])]

REB DREB
  1. 如果你想要output in an array中,那么你可以这样做:

true = df.melt(ignore_index=False).reset_index()
true[true['value']].drop('value', axis=1).values

array([['FG3a', 'BLK'],
       ['REB', 'DREB']], dtype=object)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM