import pandas as pd
data = {'drinks': [1,1,1,1,1,1,1,2,2,2,1,1,3,3,3,2,2,2], 'sex': [1,1,1,2,2,2,2,1,1,1,2,2,2,1,1,1,1,1]}
df = pd.DataFrame(data)
#print (df)
d_1 = {1 :'cola', 2 :'pepsi', 3 : 'fanta'}
d_2 = {1 :'m', 2 : 'f'}
# User wants to find key from dictionary by value so i e 'cola' = 1
# Get a list of keys from dictionary which has value that matches with any value in given list of values
def getKeys(dictOfElements, listOfValues):
listOfKeys = list()
listOfItems = dictOfElements.items()
for item in listOfItems:
if item[1] in listOfValues:
listOfKeys.append(item[0])
return listOfKeys
k = getKeys(d_1, ['cola'] )
#print (k)
# Basic way to select rows whose column value equals 'cola' == 1
print (df.loc[df['drinks'] == k[0]]) # Get the integer value
# Output:
'''
drinks sex
0 1 1
1 1 1
2 1 1
3 1 2
4 1 2
5 1 2
6 1 2
10 1 2
11 1 2
'''
# doing it from list output k with multiple values. Lets try 'cola' == 1 and 'fanta' == 3
k = getKeys(d_1, ['cola', 'fanta'] )
print (df[df.drinks.isin(k)])
# Output:
'''
drinks sex
0 1 1
1 1 1
2 1 1
3 1 2
4 1 2
5 1 2
6 1 2
10 1 2
11 1 2
12 3 2
13 3 1
14 3 1
'''