C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Boolean indexingBoolean indexing is defined as a very important feature of numpy, which is frequently used in pandas. Its main task is to use the actual values of the data in the DataFrame. We can filter the data in the boolean indexing in different ways, which are as follows:
Example1This example shows the working of how to access the DataFrame with a boolean index: # importing pandas as pd import pandas as pd # dictionary of lists dict = {'name':["Smith", "William", "Phill", "Parker"], 'age': ["28", "39", "34", "36"]} info = pd.DataFrame(dict, index = [True, True, False, True]) print(info) Output: name age True Smith 28 True William 39 False Phill 34 True Parker 36 Example2This example shows the working of how to access the DataFrame with a boolean index by using .loc[] # importing pandas as pd import pandas as pd # dictionary of lists dict = {'name':["Smith", "William", "Phill", "Parker"], 'age': ["28", "39", "34", "36"]} info = pd.DataFrame(dict, index = [True, True, False, True]) # accessing a dataframe using .loc[] function print(info.loc[True]) Output: name age True Smith 28 True William 39 True Parker 36
Next TopicConcatenating data
|