Python Tutorial : Indexing DataFrames

Veröffentlicht am: 22 März 2020
auf dem Kanal: DataCamp
2,189
20

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.

---

Pandas DataFrames provide several flexible means of indexing into subsets of our data.

Let's start by importing pandas.

We then read in a small DataFrame of sales data.

The simplest indexing style uses square brackets, just as we would use to index Python lists and NumPy arrays.

For example, in the code shown here, we select with left bracket, column label, right bracket first, followed by left bracket, row label, right bracket.

Thus, here we select salt sold in January.

Columns may also be referred to as attributes of the DataFrame if their labels are valid Python identifiers.

Here we select eggs sold in March with df, dot, eggs, left bracket, Mar, right bracket.

A more efficient and more programmatically reusable way of accessing data in a DataFrame is by using accessors.

These include dot loc and dot iloc.

The difference between dot loc and dot iloc accessors is that the former accesses by using labels, the latter using index positions.

Both accessors use left bracket, row specifier, comma, column specifier, right bracket as syntax.

For instance, here we select spam sold in May using df dot loc.

On the other hand, here we select the same entry using dot iloc 4,2.

Remember, lists and array-type structures in Python use zero-based indexing.

When using bracket-indexing without the dot loc or dot iloc accessors, the result returned can be an individual value, a Pandas Series, or a Pandas DataFrame.

To ensure the return value is a DataFrame, use a nested list within square brackets.

As an example, this call uses a list of column labels (salt and eggs) to subselect a DataFrame with those two columns only.

Notice we swapped the order of these two columns (relative to the ordering in the original DataFrame) in the selection.

Use the exercises to get some practice now in basic indexing with dot loc, dot iloc, and with brackets.

#DataCamp #PythonTutorial #ManipulatingDataFrameswithpandas


Auf dieser Seite können Sie das Online-Video Python Tutorial : Indexing DataFrames mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer DataCamp 22 März 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,189 Mal angesehen und es wurde von 20 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!