Introduction
This article is another article focusing data selection using loc function available in DataFrame variable. So, the selection in this article will be selecting several rows with several columns. Actually, the process in this context will be in an open source web-based application. The application is Jupyter Notebook so run it first. After running the application, execute the script to import the data and then store it into a DataFrame variable. So, first of all run the Jupyter Notebook as follows :
(myenv) C:\python\data-science>jupyter notebook [I 17:08:28.062 NotebookApp] Serving notebooks from local directory: C:\python\data-science [I 17:08:28.062 NotebookApp] The Jupyter Notebook is running at: [I 17:08:28.067 NotebookApp] http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b [I 17:08:28.068 NotebookApp] or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b [I 17:08:28.070 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 17:08:28.225 NotebookApp] To access the notebook, open this file in a browser: file:///C:/Users/Personal/AppData/Roaming/jupyter/runtime/nbserver-8796-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
After successfully run the application, just type the following script to import data :
import pandas as pd df = pandas.read_csv("file_csv.csv", index_col="label_name")
Select Several Rows of Several Columns with loc function from a DataFrame
Just change the above pattern accordingly to achieve the purpose. Not only the value of the file name but also the value of the index_col. The following is an example for retrieving data from a CSV file. The CSV file itself contains NBA player names as an example. The following is the process to get the data and then store it into a DataFrame variable :
Move on to the next process which is selecting several rows with several columns. The following is the script to achieve it using loc function. The function itself is a function only available in a DataFrame variable. Below is the pattern :
df.loc['row_selection','column_selection']
In order to select several rows, just pass the argument in the form of several rows selection to the loc function. In this case, it must be in a form of dictionary. So, the following is the example of it :
There are two rows with two columns available as output above. The first argument is the dictionary of the values of the rows. The second argument is the dictionary of the labels of the columns.