Accessing pandas DataFrames
1. Accessing pandas DataFrames
In this lesson, we're going to go into more depth accessing data in pandas DataFrames. Let's get started.2. Selecting multiple dataframe columns
You've learned how to access single columns in a pandas DataFrame by passing the column name into the DataFrame with the square brackets. You can also access multiple columns by passing in a list of the columns you wish to select.3. Selecting rows
You can also select individual rows in a DataFrame. Since the square brackets are reserved for selecting columns, you need to use an attribute to select rows. There are two ways to select rows in pandas: using the pandas' index with the attribute "loc" or using Python indexing with the attribute "iloc".4. Selecting rows using pandas indexing
Recall that the pandas' index contains unique labels for each row. Even if the DataFrame is re-ordered, the labels will still correspond to the same data. You can access a row by passing in the pandas' index into the "loc" attribute using the square brackets. For example, here I've retrieved the data on pet ownership for Massachusetts by using the ".loc" attribute, then passing in the string "Massachusetts" into the square brackets.5. Selecting rows using Python indexing
Sometimes you may wish to ignore the labels and instead use the absolute position of the row. You can use Python indexing to get the first, second, last, or whatever row from the DataFrame by using the ".iloc" attribute. Here, I've retrieved the 18th row of the DataFrame by passing the value 17 into the ".iloc" attribute.6. Let's practice!
Now that you know how to select data in pandas DataFrames let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.