Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Using iloc | The Very First Steps
Pandas First Steps
course content

Kursinnhold

Pandas First Steps

Pandas First Steps

1. The Very First Steps
2. Reading Files in Pandas
3. Analyzing the Data

book
Challenge: Using iloc

The DataFrame we are working with:

You can also use negative indexing to access rows in the DataFrame. Negative indexing starts from the end of the DataFrame: index -1 points to the last row, -2 to the second to last, and so on.

To access the seventh row (which refers to Latvia), you can use either index 6 or -1.

123456
import pandas countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pandas.DataFrame(countries_data) # Accessing to the seventh row using negative indexing print(countries.iloc[-1])
copy

Running the above code will return the row highlighted in the image below:

Oppgave

Swipe to start coding

You are given a DataFrame named audi_cars.

  • Display all the details from the DataFrame for the 'Audi A1' model from the year 2017 and store the result in the audi_A1_2017 variable.

  • Display all the details from the DataFrame for the 'Audi A1' model from the year 2016 and store the result in the audi_A1_2016 variable.

  • Display all the details from the DataFrame for the Audi A3 model and store the result in the audi_A3 variable.

Task Table

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 14
toggle bottom row

book
Challenge: Using iloc

The DataFrame we are working with:

You can also use negative indexing to access rows in the DataFrame. Negative indexing starts from the end of the DataFrame: index -1 points to the last row, -2 to the second to last, and so on.

To access the seventh row (which refers to Latvia), you can use either index 6 or -1.

123456
import pandas countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pandas.DataFrame(countries_data) # Accessing to the seventh row using negative indexing print(countries.iloc[-1])
copy

Running the above code will return the row highlighted in the image below:

Oppgave

Swipe to start coding

You are given a DataFrame named audi_cars.

  • Display all the details from the DataFrame for the 'Audi A1' model from the year 2017 and store the result in the audi_A1_2017 variable.

  • Display all the details from the DataFrame for the 'Audi A1' model from the year 2016 and store the result in the audi_A1_2016 variable.

  • Display all the details from the DataFrame for the Audi A3 model and store the result in the audi_A3 variable.

Task Table

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 14
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt