Search code examples
pythonpandasdata-analysis

Pandas DataFrame as lookup table with embedded lists


I have a dataframe with the following structure:

     A          B
    [1, 2, 3] [a, b, c]
    [4, 5, 6] [d, e, f]

I want to query the dataframe such that when I input 1, it should return [a,b,c]. Similarly, querying for 6 should return [d, e, f]. What's the most readable way to do it?


Solution

  • Use map and loc

    n = 1
    df.loc[df.A.map(lambda x: n in x), 'B']
    
    Out[209]:
    0    [a, b, c]
    Name: B, dtype: object