Home > Enterprise >  Get index and column name for a particular value in Pandas Dataframe
Get index and column name for a particular value in Pandas Dataframe

Time:08-21

I have the following 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

Now I want to search for a value in this DataFrame, and store the index and column name in 2 different variables.

For example:
If I search "Consignee", I should get

index = 5
column = 'A'

CodePudding user response:

Assuming you really want the index/column of the match, you can use a mask and stack:

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

output:

5  A    Consignee
dtype: object

As list:

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

output: [(5, 'A')]

  • Related