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
EasyThe CSV file stats.csv contains information about stats.
- Extract only the
evenids (rows with ids2,4,6, ...) - Extract only the
ID,SKILL, &SKILL_POINTScolumns. - Change the columns to lower-case names.
- Sort the results by
skill_pointsin 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
4Data Analysis with Pandas
Descriptive StatisticsGrouping and Aggregating DataDifferent AggregationsMerge & Concat2Working with the DataFrame
Understanding DataFramesAccessing DataData Cleaning - Missing dataData Cleaning - More tools3Data Manipulation with Pandas
Return Requested ResultFilter DataAdd & DeleteModify DataModify StringsCustom Modifications