1

I have this dataframe .

      drinks  sex
0        1    1
1        1    1
2        1    1
3        1    2
4        1    2
5        1    2
6        1    2
7        2    1
8        2    1
9        2    1
10       1    2
11       1    2
12       3    2
13       3    1
14       3    1
15       2    1
16       2    1
17       2    1

And these two dictionaries:

d_1 = {1 :'cola', 2 :'pepsi', 3 : 'fanta'}

d_2 = {1 :'m', 2 : 'f'} 

I want to filter the values in my dataframe, but by linking the keys of dictionary. For ex if i give:

df[ df['drinks'] == 'cola'] 

then the output should look like this:

drinks  sex

1       1
1       1
1       1
1       2
1       2
1       2
1       2
1       2
1       2 

The idea is here not to give the values of dataframe column but to give the values of dictionary and it should filter the values in dataframe. I tried writing a function but didn't work. Any idea, how to acheive this. thanks!!!!

Ajax
  • 159
  • 7
  • 2
    I think instead of `{1 :'cola', 2 :'pepsi', 3 : 'fanta'}`, `{'cola' : 1, 'pepsi' : 2, 'fanta' : 3}` would have been more convenient. Then you could do: `df[ df['drinks'] == d_1['cola']] ` – Sayandip Dutta Mar 10 '20 at 17:31
  • yeah, but i want to do it with the help of function. Just a idea how to achieve this – Ajax Mar 10 '20 at 17:34

3 Answers3

3

This is replace with a dictionary:

d = dict(zip([*df],[d_1,d_2]))
m = df.replace(d)
output = df[m['drinks']=='cola']
#you can now operate on both columns like : df[(m['drinks']=='cola') & (m['sex']=='m')]

    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

Where m is:

print(m)

    drinks sex
0    cola   m
1    cola   m
2    cola   m
3    cola   f
4    cola   f
5    cola   f
6    cola   f
7   pepsi   m
8   pepsi   m
9   pepsi   m
10   cola   f
11   cola   f
12  fanta   f
13  fanta   m
14  fanta   m
15  pepsi   m
16  pepsi   m
17  pepsi   m

and the dictionary looks like:

print(d)
#{'drinks': {1: 'cola', 2: 'pepsi', 3: 'fanta'}, 'sex': {1: 'm', 2: 'f'}}
anky
  • 74,114
  • 11
  • 41
  • 70
1

You can map with the dictionary and then perform boolean indexing as:

df[df.drinks.map(d_1).eq('cola')]

      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

And the same would apply for d_2, you'd just have to map with the it instead of d_1

yatu
  • 86,083
  • 12
  • 84
  • 139
  • Can i write this expression in function, so that the dictionary name and key also have been asked. – Ajax Mar 10 '20 at 17:38
  • Sure. Just add them as parameters and change the dictionary and `string` accordingly @Arpit – yatu Mar 10 '20 at 17:39
0
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
'''