Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas provide a unique method to retrieve rows from a Data frame.
DataFrame.loc method is a method that takes only index labels and returns row or dataframe if the index label exists in the caller data frame.
Index label: String or list of string of index label of rows
Return type: Data frame or Series depending on parameters
To download the CSV used in code, click here.
Example #1: Extracting single Row
In this example, Name column is made as the index column and then two single rows are extracted one by one in the form of series using index label of rows.
As shown in the output image, two series were returned since there was only one parameter both of the times.
Example #2: Multiple parameters
In this example, Name column is made as the index column and then two single rows are extracted at the same time by passing a list as parameter.
As shown in the output image, this time the data type of returned value is a data frame. Both of the rows were extracted and displayed like a new data frame.
Example #3: Extracting multiple rows with same index
In this example, Team name is made as the index column and one team name is passed to .loc method to check if all values with same team name have been returned or not.
As shown in the output image, All rows with team name “Utah Jazz” were returned in the form of a data frame.
Example #4: Extracting rows between two index labels
In this example, two index label of rows are passed and all the rows that fall between those two index label have been returned (Both index labels Inclusive).
As shown in the output image, all the rows that fall between passed two index labels are returned in the form of a data frame.
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Python | Extracting rows using Pandas .iloc
- Python | Pandas Series.loc
- Python | Pandas DataFrame.loc
- Difference between loc() and iloc() in Pandas DataFrame
- Extracting MAC address using Python
- Extracting email addresses using regular expressions in Python
- Using CountVectorizer to Extracting Features from Text
- Python - Extracting Kth Key in Dictionary
- Python - Extracting keys not in values
- Python - Extracting Key from Value Substring
- Python - Extracting Priority Elements in Tuple List
- Extracting an attribute value with beautifulsoup in Python
- NLP | Extracting Named Entities
- Extracting the real and imaginary parts of an NumPy array of complex numbers
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
- How to skip rows while reading csv file using Pandas?
- Concatenate strings from several rows using Pandas groupby
- How to Filter rows using Pandas Chaining?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai