在Pandas DataFrame中获取特定值的索引和列名

3

我有一个Pandas DataFrame,内容如下:

                             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

现在我想在这个 DataFrame 中搜索一个值,并将索引和列名分别存储在两个变量中。

例如:
如果我搜索"收货人",我应该得到

index = 5
column = 'A'
1个回答

5

假设您真的想要匹配的索引/列,您可以使用掩码和stack函数:

df.where(df.eq('Consignee')).stack()

输出:

5  A    Consignee
dtype: object

列表形式:

df.where(df.eq('Consignee')).stack().index.tolist()

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


谢谢,我将使用result[0][0]和result[0][1]获取索引和列名。 - Divyansh Gemini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接