{"id":26357,"date":"2021-12-21T09:28:04","date_gmt":"2021-12-21T03:58:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=26357"},"modified":"2021-12-21T09:28:04","modified_gmt":"2021-12-21T03:58:04","slug":"python-loc-function-to-extract-values-from-a-dataset","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-loc-function-to-extract-values-from-a-dataset\/","title":{"rendered":"Python loc() Function: To Extract Values from a Dataset"},"content":{"rendered":"
Python loc() Function:<\/strong><\/p>\n Python is made up of modules that provide built-in functions for dealing with and manipulating data values.<\/p>\n Pandas is an example of such a module.<\/p>\n The Pandas module allows us to manage enormous data sets including a massive amount of data for processing all at once.<\/p>\n This is where Python’s loc() method comes into play. The loc() function makes it simple to retrieve data values from a dataset.<\/p>\n The loc()<\/strong> function allows us to obtain the data values fitted in a specific row or column based on the index value given to the function.<\/p>\n Syntax:<\/strong><\/p>\n We must supply the index values for which we want the whole data set to be shown in the output.<\/p>\n The index label could be one of the following values:<\/p>\n Using the loc() function, we may extract a specific record from a dataset depending on the index label.<\/p>\n If the provided index\u00a0is not present as a label\u00a0it returns KeyError.<\/p>\n Example<\/strong><\/p>\n Output:<\/strong><\/p>\n Extraction of a Row from the Given Dataframe<\/strong><\/p>\n Get all of the data values linked with the index label ‘clusters’ as shown below:<\/p>\n Output:<\/strong><\/p>\n Extraction of Multiple Rows from the Given Dataframe<\/strong><\/p>\n We cal also get the multiple rows from the given dataframe.<\/p>\n Get all of the data values linked with the index labels ‘clusters’,\u00a0 ‘Almond Delight’ as shown below:<\/p>\n Output:<\/strong><\/p>\n Extraction of Range of Rows from the Given Dataframe<\/strong><\/p>\n We can retrieve data values of the range of rows using the loc[] function and slicing operator as shown below:<\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python loc() Function: Python is made up of modules that provide built-in functions for dealing with and manipulating data values. Pandas is an example of such a module. The Pandas module allows us to manage enormous data sets including a massive amount of data for processing all at once. This is where Python’s loc() method …<\/p>\npandas.DataFrame.loc[index label]<\/pre>\n
\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Pass the some random list of data given to the DataFrame() function and store it in a variable\r\ngvn_data = pd.DataFrame([[110, 2, 25, 14], [100, 3, 22, 10], [115, 1, 27, 9], [90, 5, 12, 14]],\r\n index=['Almond Delight', 'Clusters', 'Corn Chex', 'Cocoa Puffs'],\r\n columns=['calories', 'vitamins', 'fats','carboydrates'])\r\n# Print the above dataframe\r\nprint(\"The given input Dataframe: \")\r\nprint(gvn_data)<\/pre>\n
The given input Dataframe: \r\n calories vitamins fats carboydrates\r\nAlmond Delight 110 2 25 14\r\nClusters 100 3 22 10\r\nCorn Chex 115 1 27 9\r\nCocoa Puffs 90 5 12 14<\/pre>\n
print(gvn_data.loc['Clusters'])<\/pre>\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Pass the some random list of data given to the DataFrame() function and store it in a variable\r\ngvn_data = pd.DataFrame([[110, 2, 25, 14], [100, 3, 22, 10], [115, 1, 27, 9], [90, 5, 12, 14]],\r\n index=['Almond Delight', 'Clusters', 'Corn Chex', 'Cocoa Puffs'],\r\n columns=['calories', 'vitamins', 'fats','carboydrates'])\r\n# Get all of the data values linked with the index label 'Clusters' using the\r\n# loc[] function and print it.\r\nprint(gvn_data.loc['Clusters'])<\/pre>\n
calories 100\r\nvitamins 3\r\nfats 22\r\ncarboydrates 10\r\nName: Clusters, dtype: int64<\/pre>\n
print(gvn_data.loc[['Clusters', 'Almond Delight']])<\/pre>\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Pass the some random list of data given to the DataFrame() function and store it in a variable\r\ngvn_data = pd.DataFrame([[110, 2, 25, 14], [100, 3, 22, 10], [115, 1, 27, 9], [90, 5, 12, 14]],\r\n index=['Almond Delight', 'Clusters', 'Corn Chex', 'Cocoa Puffs'],\r\n columns=['calories', 'vitamins', 'fats','carboydrates'])\r\n# Extracting multiple rows from the given dataframe.\r\n# Get all of the data values linked with the index labels 'clusters',\u00a0 'Almond Delight'\r\n# using the loc[] function and print it.\r\nprint(gvn_data.loc[['Clusters', 'Almond Delight']])<\/pre>\n
calories vitamins fats carboydrates\r\nClusters 100 3 22 10\r\nAlmond Delight 110 2 25 14<\/pre>\n
print(gvn_data.loc['Clusters': 'Cocoa Puffs'])<\/pre>\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Pass the some random list of data given to the DataFrame() function and store it in a variable\r\ngvn_data = pd.DataFrame([[110, 2, 25, 14], [100, 3, 22, 10], [115, 1, 27, 9], [90, 5, 12, 14]],\r\n index=['Almond Delight', 'Clusters', 'Corn Chex', 'Cocoa Puffs'],\r\n columns=['calories', 'vitamins', 'fats','carboydrates'])\r\n# Extracting range of rows from the given dataframe.\r\n# Get all of the data values linked with the index labels 'clusters' to 'Cocoa Puffs '\r\n# using the loc[] function,slicing operator and print it.\r\nprint(gvn_data.loc['Clusters': 'Cocoa Puffs'])<\/pre>\n
calories vitamins fats carboydrates\r\nClusters 100 3 22 10\r\nCorn Chex 115 1 27 9\r\nCocoa Puffs 90 5 12 14<\/pre>\n