Python Tutorial : Indexing DataFrames

Pubblicato il: 22 marzo 2020
sul canale di: 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


In questa pagina del sito puoi guardare il video online Python Tutorial : Indexing DataFrames della durata di ore minuti seconda in buona qualità , che l'utente ha caricato DataCamp 22 marzo 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 2,189 volte e gli è piaciuto 20 spettatori. Buona visione!