Want to learn more? Take the full course at https://learn.datacamp.com/courses/ma... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Let's look now at slicing DataFrames.
Remember, we're still using our sales DataFrame.
We assume that pandas has been imported as pd.
The basic indexing here picks a column by default.
The result returned is actually a Pandas Series.
A Series is a one-dimensional array with a labelled index (like a hybrid between a NumPy array and a dictionary).
Another way to think of a DataFrame is a labelled two-dimensional array with Series for columns sharing common row labels.
Slicing can be performed with or without accessors.
Having worked with Python lists, we are familiar with the colon syntax for slicing.
For instance, we remember positional slicing is half-open, so slicing one, colon, four extracts positions one, two, and three (indexed from zero).
Thus, here we extract the eggs column as a Series and then select those three elements from the Series.
Pandas extends this colon syntax to allow labels in slices.
Here, the first colon selects all rows.
The slice 'eggs':'salt' selects both columns eggs and salt.
This is a potential gotcha: slicing with labels and the dot loc accessor includes the right end-point (unlike positional slicing seen so far).
This example is similar in using dot loc to slice all columns and some rows.
The first slice 'Jan':'Apr' extracts all four rows corresponding to January, Ferbruary, March, and April inclusive.
The second bare colon is a universal slice selecting all columns.
This example extracts a block with a proper subset of rows and columns (from March to May inclusive and from salt to spam inclusive.
Using dot iloc is very similar to dot loc, simply with positional integers specifying slices rather than labels.
Here, we extract the same slice as before using dot iloc and positions: from row 2 up to but not including row 5 and from column 1 to the last column.
Remember, omitting the explicit start or end in a slice means we slice from the beginning or to the end respectively.
Both the dot iloc and dot loc accessors can use lists in place of slices.
Here is an example using dot loc and a list of two columns.
Here is another using dot iloc and a list of three rows.
Remember, with dot iloc the column slice 0:2 selects two columns.
Here's an important subtle distinction to understand.
Selecting df, left bracket, 'eggs', right bracket yields a Series from the column labelled 'eggs'.
Selecting df, left bracket, left bracket, 'eggs', right bracket, right bracket returns the DataFrame consisting of a single column (namely 'eggs').
Many (but not all) operations are shared between DataFrames and Series, and a Series is always only one dimension of labelled data.
Now that you've learned Pandas slicing idioms, here are some exercises for you to work on.
#DataCamp #PythonTutorial #ManipulatingDataFrameswithpandas
On this page of the site you can watch the video online Python Tutorial : Slicing DataFrames with a duration of 16 minute 40 second in good quality, which was uploaded by the user DataCamp 22 March 2020, share the link with friends and acquaintances, this video has already been watched 8,744 times on youtube and it was liked by 105 viewers. Enjoy your viewing!