Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Resampling

A separate form of aggregation is time-based resampling. You can think of this practice as grouping by time periodexcept that statistics will be filled for missed time periods, too.

For example, let's count casualties for each month of the war, assuming the end of each battle as a time point. For that, we'll have to set DateTime as an index, first. For the sake of simplicity, let's create a copy of the dataframe to perform on:

ts = data[['axis killed', 'allies killed', 'end']].copy()ts = ts.set_index('end').sort_index()

Now, all we need to do is define the frequency and aggregation method, and we're good to go:

>>> timeline = ts.resample('1Y').agg('sum')>>> timeline           axis killed allies killedend 1939-12-31 23727.0     166092.01940-12-31 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content