Conteúdo do Curso
Pandas First Steps
Pandas First Steps
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.
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])
Running the above code will return the row highlighted in the image below:
Swipe to begin your solution
- Display all the details from the DataFrame for the
Audi A1
model from the year 2017. To do this, you'll need to use positive indexing. - Display all the details from the DataFrame for the
Audi A1
model from the year 2016 using negative indexing. - Display all the details from the DataFrame for the
Audi A3
model using positive indexing.
Make sure to use the iloc
attribute.
Solução
Obrigado pelo seu feedback!
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.
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])
Running the above code will return the row highlighted in the image below:
Swipe to begin your solution
- Display all the details from the DataFrame for the
Audi A1
model from the year 2017. To do this, you'll need to use positive indexing. - Display all the details from the DataFrame for the
Audi A1
model from the year 2016 using negative indexing. - Display all the details from the DataFrame for the
Audi A3
model using positive indexing.
Make sure to use the iloc
attribute.
Solução
Obrigado pelo seu feedback!