The lookup table is used for retrieving values from a database. With lookup tables, we extract data from a database so as to reduce the computations. Retrieving a value from a lookup table is a faster process compared to simple input-output operations. In python, we use dictionaries to perform a lookup table. In this article, we shall be throwing light into different ways of performing a lookup operation in python.
Preference of lookup tables
Lookup tables are used in several programming languages. With lookup tables, we can easily access values from a database. This helps in maintaining data integrity in the database system. We can also use lookup tables to validate input values in a table. When given a set of input values, with a lookup operation we can retrieve its corresponding output values from the given table or database.
Using Python Dictionaries
Lookup tables are also known as dictionaries in python. Dictionaries represent the implementation of a hash table in order to perform a lookup. Dictionaries consist of key-value pairs. With each key, its corresponding values are accessed. Lookup operations are faster in dictionaries because python implements them using hash tables. We look up the keys in the dictionary and accordingly fetch the key’s value.
Let us consider a dictionary named ‘dictionary’ containing key-value pairs. The keys are numerical values, and their values are the number’s string representation. To fetch the value, we simply lookup using the key.
dictionary = {0 : 'Zero', 1 : 'One', 2 : 'Two', 3 : 'Three' }
print(dictionary[2])
The output is:
Two
How dictionary uses a hash table for python lookup table
Hash tables are a way of implementing dictionaries. Hash tables are the data structures behind dictionaries. In hash tables, we take hash values of a key and apply the hash function to it. Depending on the key, it is mapped to the respective value bucket. Similarly, dictionaries, maps the key values for the lookup operation to their value to retrieve that information. Dictionaries are often called maps because they map the respective key-value to its value.
Lookup function in pandas
The pandas library in python contains a lookup() function. When given arrays of rows and columns, the lookup() function returns an array of values corresponding to each row-column pair.
Syntax of the lookup function
The syntax of the pandas lookup function is:
DataFrame.lookup(row_labels, col_labels)
We call the lookup() function from the pandas dataframe.
row_labels: It indicates the row labels used for lookup
col_labels: It indicates the column labels used for lookup
Return value:
It returns an n – dimensional numpy array.
Example of Pandas lookup()
Let us understand the implementation of the lookup() function in pandas with the help of an example in python. We shall take a dataframe. First, we shall import the pandas library.
import pandas as pd
We shall take a dataframe of six columns and five rows. The dataframe consists of numeric data. After creating the dataframe, we shall print the dataframe.
df = pd.DataFrame([[3, 15 , 100, 4, 90, 99], [0, 30 , 5, 75, 33, 88], [2, 16 , 8, 14, 77, 23], [1, 116 , 34, 13, 17, 44], [4, 25 , 10, 20, 6, 15]], columns=('Index', 0, 1, 2,3,4))
print(df)
The output is:
Index 0 1 2 3 4 0 3 15 100 4 90 99 1 0 30 5 75 33 88 2 2 16 8 14 77 23 3 1 116 34 13 17 44 4 4 25 10 20 6 15
Now, we shall use the lookup() function to look for values where the row and column names are identical. We shall use df.index as the dataframe index for the rows and the ‘Index’ column as the column value. Then, we will save the obtained common values into a new column named ‘new’.
For example: When index = 3, the corresponding column value in column 3, which is 90, will be the value in the ‘new’ column. Similarly, for Index = 0, the corresponding value in column 0, which is 30, will be considered. This shall apply to create the entire ‘new’ column.
df['new'] = df.lookup(df.index, df['Index'])
print(df)
The output dataframe is:
Index 0 1 2 3 4 new 0 3 15 100 4 90 99 90 1 0 30 5 75 33 88 30 2 2 16 8 14 77 23 14 3 1 116 34 13 17 44 34 4 4 25 10 20 6 15 15
Using where() for lookup
The numpy library contains a function where(). The function is used to perform lookup inside a database. It will check values if they fulfill a certain condition or not. If the condition is fulfilled, then it returns a value ‘x’, else, value ‘y’.
Syntax of where() function
The syntax for where() function is:
numpy.where(condition[, x, y])
condition: It is the condition to be fulfilled. If true, then its value will be ‘x’, else its value will be ‘y’.
x: Value if the condition is True
y: Value if the condition is False
Return Value:
out : It is an n dimensional array containing values ‘x’ and ‘y’ depending on the condition.
Example of where()
Let us consider a dataframe containing name and age of a person. First, we shall import the numpy and the pandas library.
import numpy as np
import pandas as pd
Now using Pandas, we will create a dataframe. The dataframe has four records and two columns – person and age. Then, we shall print the dataframe.
df = pd.DataFrame([['Kat', 25 ], ['Henry', 16 ], ['Liam', 18 ], ['Noah', 14 ]], columns=('Person', 'Age'))
print(df)
The output is:
Person Age 0 Kat 25 1 Henry 16 2 Liam 18 3 Noah 14
Now, we shall use the np.where() function to perform a lookup to check all the people who are eligible for voting.
The condition which we will pass inside the where() function is to check if the value of the ‘Age’ column is greater than or equal to 18 or not. It will only consider those people eligible whose age is greater than or equal to 18.
The function will return ‘Eligible’ if the condition will be fulfilled. Else it will return ‘Not eligible’. Then, we shall store the variable ‘x’ into a new column inside the dataframe named ‘Vote’.
x = np.where(df['Age']>=18,'Eligible','Not eligible')
df['Vote'] = x
print(df)
The output is:
Person Age Vote 0 Kat 25 Eligible 1 Henry 16 Not eligible 2 Liam 18 Eligible 3 Noah 14 Not eligible
That wraps up the python lookup table. If you have any doubts, let us know in the comments below.
Until next time, Keep Learning!