Search code examples
pythondataframecategorical-datalabel-encoding

Getting to know which value corresponds to a particular column value


I wish to find the exact value of the index for an input defined value of key in a dataframe, below is the code I am trying to do to get it.

data_who = pd.DataFrame({'index':data['index'], 'Publisher_Key':data['Key']})

Below is my O/P dataframe:

enter image description here

If suppose I give an input say 100 as the key value, I would like to get the O/P of the index value, which is Goat, what should I do in my code??

PS: Too many labels in the data after performing label encoding, so wanted to know the value of the labels corresponds to which category.


Solution

  • If index is a column, then you can do as follows:

    data.loc[data['Key'] == 100, 'index'].iloc[0]
    >>> 'Zebra'
    

    Or other option:

    data[data['Key'] == 100]['index'].iloc[0]
    >>> 'Zebra'
    

    If index is the index of the dataframe, replace ['index'] with .index.

    As a side note: you shouldn't name a column index in pandas, it's a concept itself and naming a column that way could be misleading.