techdoodle
techdoodle

Reputation: 115

Find a value in column of lists in pandas dataframe

I have a dataframe with two columns A & B, B is column of lists and A is a string, I want to search a value in the column B and get the corresponding value in column A. For ex :

     category         zones
0  category_1  [zn_1, zn_2]
1  category_2        [zn_3]
2  category_3        [zn_4]
3  category_4  [zn_5, zn_6]

If the input = 'zn_1', how can i get a response back as 'category_1'?

Upvotes: 0

Views: 52

Answers (1)

wwnde
wwnde

Reputation: 26676

Use str.contains and filter category values

inputvalue='zn_1'
df[df.zones.str.contains(inputvalue)]['category']

#If didnt want an array

inputvalue='zn_1'
list(df[df.zones.str.contains(inputvalue)]['category'].values)[0]

Upvotes: 2

Related Questions