Menu
Coddy logo textTech

Return Requested Result

Lesson 8 of 19 in Coddy's Pandas Analytics course.

Sometimes we want to answer a few questions, so we need to extract only the relevant columns.

As a reminder, to extract a single-column series object:

column = df['col']

To extract multiple columns, specify the column names inside an array (this returns a dataframe):

columns = df[['col1', 'col2', 'col3', ...]]

To extract only the relevant rows, we can use slicing, just like in Python lists:

rows = df[row_index_start:row_index_end]

For example:

filtered_df = df[5:9]

filtered_df will hold rows 5, 6, 7, and 8.

Now to return the result in the order we want:

res = df.sort_values(by='column_name', ascending=True)
challenge icon

Challenge

Easy

The CSV file stats.csv contains information about stats.

  • Extract only the even ids (rows with ids 2, 4, 6, ...)
  • Extract only the  ID,  SKILL, & SKILL_POINTS columns.
  • Change the columns to lower-case names.
  • Sort the results by skill_points in descending order.

Save the result in df variable.

Remember how slicing works to extract only the even rows.

Try it yourself

import pandas as pd

df = pd.read_csv("./stats.csv")

All lessons in Pandas Analytics