Inspecting Data
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 34 of 60.
A new dataset is too long to print. A few quick checks tell you its size, its columns and its values.
songs = pd.read_csv("songs.csv")
print(songs.head(3)) # the first 3 rows; head() alone gives 5
print(songs.shape) # (rows, columns)
for name in songs.columns:
print(name)This prints:
song seconds plays
0 Blue Hour 210 5169
1 Paper Moon 176 6689
2 Tide 272 2739
(24, 3)
song
seconds
plays.describe() summarizes every number column and leaves out the text column song. print(songs.describe()) prints:
seconds plays
count 24.000000 24.000000
mean 212.708333 5116.333333
std 42.367419 2189.610898
min 151.000000 1289.000000
25% 175.250000 3626.250000
50% 213.000000 5051.000000
75% 232.250000 6711.500000
max 290.000000 8992.00000025%, 50% and 75% are the values a quarter, half and three quarters up the sorted column.
The summary is a table too: "plays" in songs.describe().columns is True, since in asks whether a name is there.
Challenge
MediumA model learns only from number columns, so a taxi company checks its trip log first. The program loads taxi_trips.csv into trips and reads a number n.
- Print
Trips:and the number of rows. - For each column, in order, print its name, a colon, and
numbersif.describe()summarizes it, elsetext. - Print the first
nrows.
For a table of 30 bike rentals and n = 2:
Trips: 30
bike: text
km: numbers
station: text
bike km station
0 B-12 4.6 Pier
1 B-07 11.2 CentralTry it yourself
import pandas as pd
trips = pd.read_csv("taxi_trips.csv")
n = int(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
7pandas DataFrames
SeriesDataFramesReading CSV FilesInspecting DataSelecting ColumnsRecap: Movie Ratings2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler