Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Advanced .iloc[] | s1
Track DA with Py - Data Manipulation with pandas
Avsnitt 1. Kapitel 19
single

single

bookAdvanced .iloc[]

Svep för att visa menyn

We will learn some new features that iloc[] provides. The coolest one is that we can specify indices of both rows and columns. This attribute is similar to .loc[], but the last index of the slicing is exclusive.

Look at the example and the relevant output:

  • data.iloc[1, 2] - extracts the item located in the dataset's second row and third column. The first index corresponds to the row index, and the second to the column index. Indeed, you can skip one of them;
  • data.iloc[:, 3] - extracts all values from the rows of the fourth column 'IMDb-Rating';
  • data.iloc[3, :] or data.iloc[3] - extracts the 4th row and all relevant columns;
  • data.iloc[:2, 1:4] - extracts the first two rows and column with the indices 1, 2, 3;
  • data.iloc[[2,4],[1,3]] - extracts the rows with indices 2,4 and columns with the indices 1, 3.
Uppgift

Swipe to start coding

You are given a dataset named data. Your task is to practice selecting specific rows and columns using index-based selection.

  • Extract the first 50 rows and the columns with indices 1 and 4 from the data DataFrame.
  • Store the result in a new variable named data_extracted.
  • Finally, print the first few rows of this subset using the .head() function.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 19
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt