Computer scienceData scienceInstrumentsPandasStoring data with pandas

.loc & .iloc

Personal selection

Report a typo

Look at a sample DataFrame called chart2020. Select the artist and song columns, and the last three rows only, then print the result.

chart2020.head()

Output:

+----+--------------+-----------------+-----------+-----------+-----------+-----------+-----------+----------------+
|    | artist       | song            |   peak_us |   peak_uk |   peak_de |   peak_fr |   peak_ca |   result_place |
|----+--------------+-----------------+-----------+-----------+-----------+-----------+-----------+----------------|
|  0 | The Weeknd   | Blinding Lights |         1 |         1 |         1 |         1 |         1 |              1 |
|  1 | Dua Lipa     | Don't Start Now |         2 |         3 |        10 |        12 |         3 |              2 |
|  2 | Roddy Ricch  | The Box         |         1 |         2 |        12 |         7 |         1 |              3 |
|  3 | Post Malone  | Circles         |         1 |        19 |        37 |        77 |         3 |              4 |
|  4 | Harry Styles | Adore You       |         6 |         7 |        83 |       126 |        10 |              5 |
+----+--------------+-----------------+-----------+-----------+-----------+-----------+-----------+----------------+

Notes:

  • select columns in the specified order;
  • do not reset the indexes, they should remain.
Write a program in Python 3
# your code here. The data frame is already loaded and stored as chart2020.

print(...)
___

Create a free account to access the full topic