pandas.DataFrame.loc[] in Python
In this tutorial, we are going to learn about pandas.DataFrame.loc in Python. The loc property of pandas.DataFrame is helpful in many situations and can be used as if-then or if-then-else statements with assignments to more than one column. There are many other usages of this property. We will discuss them all in this tutorial.
Usage of pandas.DataFrame.loc
The .loc property is primarily used with labels but we can also use it with boolean arrays. Using pandas.Dataframe.loc we can access rows and columns of a data frame as per the passed labels.
The below code uses .loc property to access the third row and the third column.
import pandas dataframe = pandas.DataFrame({'L1': [1, 3, 5, 7, 9], 'L2': [2, 4, 6, 8, 10], 'L3': [12, 34, 56, 78, 910]}) #creating index ind = ['R1', 'R2', 'R3', 'R4', 'R5'] dataframe.index = ind print(dataframe) #using loc to print the value at R3 and L3 print() print("Value at R3 and L3") print(dataframe.loc['R3', 'L3'])
It gives the output:
L1 L2 L3 R1 1 2 12 R2 3 4 34 R3 5 6 56 R4 7 8 78 R5 9 10 910 Value at R3 and L3 56
We can also pass a list of labels with loc as given in the below code.
import pandas dataframe = pandas.DataFrame({'L1': [1, 3, 5, 7, 9], 'L2': [2, 4, 6, 8, 10], 'L3': [12, 34, 56, 78, 910], 'L4': [11, 22, 33, 44, 55]}) #creating index ind = ['R1', 'R2', 'R3', 'R4', 'R5'] dataframe.index = ind print() print("Printing R1 and R2 values for the labels L1 and L3") print(dataframe.loc[['R1','R2'], ['L1','L3']])
Output:
Printing R1 and R2 values for the labels L1 and L3 L1 L3 R1 1 12 R2 3 34
We can also pass a slice object in pandas.DataFrame.loc[] as shown here. The below code prints the values at R1 and R2 for the labels L1, L2 and L3.
import pandas dataframe = pandas.DataFrame({'L1': [1, 3, 5, 7, 9], 'L2': [2, 4, 6, 8, 10], 'L3': [12, 34, 56, 78, 910], 'L4': [11, 22, 33, 44, 55]}) #creating index ind = ['R1', 'R2', 'R3', 'R4', 'R5'] dataframe.index = ind print() print("Printing R1, R2 and R3 values for the labels L2, L3 and L4") print(dataframe.loc['R1':'R3', 'L2':'L4'])
Output:
Printing R1, R2 and R3 values for the labels L2, L3 and L4 L2 L3 L4 R1 2 12 11 R2 4 34 22 R3 6 56 33
As you can notice, unlike normal slice objects in Python, slicing for the .loc property includes both start and stop labels.
Passing Boolean arrays
We can also pass a boolean array containing values True and False in the loc property. See the below program to understand it in a better way.
import pandas dataframe = pandas.DataFrame({'L1': [1, 3, 5, 7, 9], 'L2': [2, 4, 6, 8, 10], 'L3': [12, 34, 56, 78, 910], 'L4': [11, 22, 33, 44, 55]}) #creating index ind = ['R1', 'R2', 'R3', 'R4', 'R5'] dataframe.index = ind print() print("Printing R4, R5 values for L3 and L4") print(dataframe.loc[[False, False, False, True, True], [False, False, True, True]])
Output:
Printing R4, R5 values for L3 and L4 L3 L4 R4 78 44 R5 910 55
If-else in pandas.DataFrame.loc[]
We can use pandas.DataFrame.loc to execute if-else statements and can assign values accordingly to one or more columns. The below code illustrates this concept clearly. Have a look at the code.
import pandas dataframe = pandas.DataFrame({'L1': [1, 3, 5, 7, 9], 'L2': [2, 4, 6, 8, 10], 'L3': [12, 34, 56, 78, 910], 'L4': [11, 22, 33, 44, 55]}) #creating index ind = ['R1', 'R2', 'R3', 'R4', 'R5'] dataframe.index = ind print() print("Changing values of L2 to 0 if L3 is greater than 50.") dataframe.loc[dataframe.L3 > 50, 'L2'] = 0 print(dataframe)
The output of the above code:
Changing values of L2 to 0 if L3 is greater than 50. L1 L2 L3 L4 R1 1 2 12 11 R2 3 4 34 22 R3 5 0 56 33 R4 7 0 78 44 R5 9 0 910 55
To know more about the topic, read this.
Thank you.
Also read: Pandas.DataFrame.iloc in Python
Leave a Reply