Challenge: Using iloc[]
The DataFrame you 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.
123456import 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 start coding
You are given a DataFrame named audi_cars.
- Select the entire row (all columns) for the
'Audi A1'model from the year 2017 and store it inaudi_A1_2017. - Do the same for the
'Audi A1'model from the year 2016 and store it inaudi_A1_2016. - Finally, select the
'Audi A3'model and store it inaudi_A3.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.03
Challenge: Using iloc[]
Swipe to show menu
The DataFrame you 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.
123456import 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 start coding
You are given a DataFrame named audi_cars.
- Select the entire row (all columns) for the
'Audi A1'model from the year 2017 and store it inaudi_A1_2017. - Do the same for the
'Audi A1'model from the year 2016 and store it inaudi_A1_2016. - Finally, select the
'Audi A3'model and store it inaudi_A3.
Solution
Thanks for your feedback!
single