How to Access Rows by Name in Pandas (Python)

DataDaft
DataDaft
3.7 هزار بار بازدید - 4 سال پیش - ↓  Code Available Below!  ↓
↓  Code Available Below!  ↓

This video shows how to access specific rows of a pandas data frame using the index names of the rows. Data frames don't always have text row names assigned to the columns, but when they do, they can be used to access specific rows instead of using numeric indexing.

If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.seevid.ir/c/DataDaft?su...


Code used in this Python Code Clip:

import pandas as pd

import statsmodels.api as sm  #(To access mtcars dataset)
mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data
mtcars.head()

Access a row by by name with .loc
mtcars.loc["Datsun 710"]

Access multiple rows by by name
mtcars.loc[["Datsun 710", "Hornet 4 Drive", "Mazda RX4"]]

Access multiple rows and specific columns by name
mtcars.loc[["Datsun 710", "Hornet 4 Drive", "Mazda RX4"], ["mpg","cyl","hp"]]


* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .


⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: https://www.kite.com/get-kite/?utm_me...
4 سال پیش در تاریخ 1399/08/14 منتشر شده است.
3,784 بـار بازدید شده
... بیشتر