获取 Pandas Dataframe 中特定值的索引和列名称

Div*_*ini 3 python dataframe pandas

我有以下熊猫数据框:

                             A                                        B
0                     Exporter                       Invoice No. & Date
1                 ABC PVT LTD.        ABC/1234/2022-23 DATED 20/08/2022
2                 1234/B, XYZ,
3           ABCD, DELHI, INDIA               Proforma Invoice No. Date.
4                                    AB/CDE/FGH/2022-23/1234 20.08.2022
5                    Consignee          Buyer (If other than consignee)
6                      ABC Co.
8            P.O BOX NO. 54321
9              Berlin, Germany
Run Code Online (Sandbox Code Playgroud)

现在我想在此 DataFrame 中搜索一个值,并将索引和列名存储在 2 个不同的变量中。

例如:
如果我搜索“Consignee”,我应该得到

index = 5
column = 'A'
Run Code Online (Sandbox Code Playgroud)

moz*_*way 5

假设您确实想要匹配的索引/列,您可以使用掩码和stack

df.where(df.eq('Consignee')).stack()
Run Code Online (Sandbox Code Playgroud)

输出:

5  A    Consignee
dtype: object
Run Code Online (Sandbox Code Playgroud)

如列表:

df.where(df.eq('Consignee')).stack().index.tolist()
Run Code Online (Sandbox Code Playgroud)

输出:[(5, 'A')]