Главная страница » С помощью какого метода можно построить сводную таблицу в pandas

С помощью какого метода можно построить сводную таблицу в pandas

  • автор:

pandas.pivot_table#

Create a spreadsheet-style pivot table as a DataFrame.

The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on the index and columns of the result DataFrame.

Parameters data DataFrame values column to aggregate, optional index column, Grouper, array, or list of the previous

If an array is passed, it must be the same length as the data. The list can contain any of the other types (except list). Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values.

columns column, Grouper, array, or list of the previous

If an array is passed, it must be the same length as the data. The list can contain any of the other types (except list). Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values.

aggfunc function, list of functions, dict, default numpy.mean

If list of functions passed, the resulting pivot table will have hierarchical columns whose top level are the function names (inferred from the function objects themselves) If dict is passed, the key is column to aggregate and value is function or list of functions.

fill_value scalar, default None

Value to replace missing values with (in the resulting pivot table, after aggregation).

margins bool, default False

Add all row / columns (e.g. for subtotal / grand totals).

dropna bool, default True

Do not include columns whose entries are all NaN. If True, rows with a NaN value in any column will be omitted before computing margins.

margins_name str, default ‘All’

Name of the row / column that will contain the totals when margins is True.

observed bool, default False

This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers.

Changed in version 0.25.0.

Specifies if the result should be sorted.

New in version 1.3.0.

An Excel style pivot table.

Pivot without aggregation that can handle non-numeric data.

Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.

Wide panel to long format. Less flexible but more user-friendly than melt.

Reference the user guide for more examples.

This first example aggregates values by taking the sum.

We can also fill missing values using the fill_value parameter.

The next example aggregates by taking the mean across multiple columns.

We can also calculate multiple types of aggregations for any given value column.

Сводные таблицы в Pandas — швейцарский нож для аналитиков

Привет, Хабр! Меня зовут Панчин Денис, это мой первый пост и я хочу Вам рассказать о работе с сводными таблицами в Pandas. Сводные таблицы хорошо известны всем аналитикам по Excel. Это прекрасный инструмент, который помогает быстро получить различную информацию по массиву данных. Рассмотрим реализацию и тонкости сводных таблиц в Pandas.

Блокнот можно открыть здесь.

Использовать будем только столбцы ‘region’ (субъект РФ), ‘municipality’ (муниципальное образование), ‘year’ (год), ‘birth’ (число родившихся на 1000 человек населения), ‘wage’ (cреднемесячная номинальная начисленная заработная плата, руб.). Сразу оговорюсь, что Москва и Санкт-Петербург являются отдельными субъектами Российской Федерации и в этом датафрейме отсутствуют.

Минимальную статистику можно получить использовав метод describe(include = ‘all’). Мы видим что у нас 4109 строки, по 81 региону и 202 городам. Средняя рождаемость на 1000 человек 11,39, минимальная — 3,4, максимальная — 36,1.

Агрегирование

Если объяснять простыми словами, то агрегирование — это процесс приведения некого большого массива значений к некому сжатому по определенному параметру массиву значений. Например, среднее, медиана, количество, сумма.

Groupby

Но средняя температура по больнице нам не интересна, мы хотим знать победителей в лицо. Допустим нам нужно посмотреть средние значения с группировкой по городам и субъектам РФ. Для этого закономерно используем метод groupby([‘region’, ‘municipality’]).agg(‘mean’).

Обратите внимание, что колонки region и municipality стали индексами.

На этом мы не успокаиваемся и пытаемся выжать больше стат.данных: среднее, медиану, минимум, максимум.

Посмотрим топ городов по зарплатам.

А что если посмотреть данные в разрезе по годам.

Pivot table

Ответим на этот вопрос чуть позже, а пока создадим сводную таблицу аналогичную той, что мы уже делали ранее.

Уже на этом этапе видно, что сводная таблица достаточно умная и сама агрегировала данные и посчитала средние значения.

Но если мы захотим расширить таблицу новыми значениями — медианой, то увидим, что конечный результат по структуре будет отличаться от того, что мы делали при использовании метода groupby.

Так мы плавно подошли к тем преимуществам, которые делает сводные таблицы швейцарским ножом для аналитиков. Если используя groupby мы «укрупняли» строки по городам, то сейчас мы можем «развернуть» столбец, например, ‘year’ (год) и посмотреть данные в разрезе по годам.

И, конечно, данные в сводной таблице можно фильтровать. Создадим сводную таблицу и оставим в ней данные по городам, в которых рождаемость на 1000 человек превышает 12, зарплата выше 40.000 и отсортируем всё по убыванию рождаемости.

Pivot

Всё было замечательно, но в Pandas кроме pivot_table есть ещё просто pivot. Посмотрим что это за зверь и чем они отличаются.

Создадим pivot: рождаемость в разрезе по регионам и годам.

Мы получили ошибку «Index contains duplicate entries, cannot reshape«. Что-то не так с индексами, попробуем создать pivot с индексами по городам, а не регионам.

Всё получилось. Как мы уже заметили всё дело в индексах. На самом деле всё в повторяющихся индексах. Как мы видели ранее в датафрейме есть позиции с несколькими городами в рамках одного субъекта, так получаются дублированные данные индексов с которыми не умеет работать pivot.

Вывод

Groupby, pivot и pivot_table удобные и привычные инструменты для работы с данными. Groupby позволяет кодом в одну строку получить агрегированные и сортированные данные, а pivot и pivot_table работать в более глубоком разрезе. Pivot_table предпочтителен, т.к. не ограничивает вас в уникальности значений в столбце индекса. И, конечно, все эти данные можно фильтровать под ваши запросы.

Reshaping and pivot tables#

../_images/reshaping_pivot.png

Data is often stored in so-called “stacked” or “record” format:

To select out everything for variable A we could do:

But suppose we wish to do time series operations with the variables. A better representation would be where the columns are the unique variables and an index of dates identifies individual observations. To reshape the data into this form, we use the DataFrame.pivot() method (also implemented as a top level function pivot() ):

If the values argument is omitted, and the input DataFrame has more than one column of values which are not used as column or index inputs to pivot() , then the resulting “pivoted” DataFrame will have hierarchical columns whose topmost level indicates the respective value column:

You can then select subsets from the pivoted DataFrame :

Note that this returns a view on the underlying data in the case where the data are homogeneously-typed.

pivot() will error with a ValueError: Index contains duplicate entries, cannot reshape if the index/column pair is not unique. In this case, consider using pivot_table() which is a generalization of pivot that can handle duplicate values for one index/column pair.

Reshaping by stacking and unstacking#

../_images/reshaping_stack.png

Closely related to the pivot() method are the related stack() and unstack() methods available on Series and DataFrame . These methods are designed to work together with MultiIndex objects (see the section on hierarchical indexing ). Here are essentially what these methods do:

stack() : “pivot” a level of the (possibly hierarchical) column labels, returning a DataFrame with an index with a new inner-most level of row labels.

unstack() : (inverse operation of stack() ) “pivot” a level of the (possibly hierarchical) row index to the column axis, producing a reshaped DataFrame with a new inner-most level of column labels.

The clearest way to explain is by example. Let’s take a prior example data set from the hierarchical indexing section:

The stack() function “compresses” a level in the DataFrame columns to produce either:

A Series , in the case of a simple column Index.

A DataFrame , in the case of a MultiIndex in the columns.

If the columns have a MultiIndex , you can choose which level to stack. The stacked level becomes the new lowest level in a MultiIndex on the columns:

With a “stacked” DataFrame or Series (having a MultiIndex as the index ), the inverse operation of stack() is unstack() , which by default unstacks the last level:

../_images/reshaping_unstack_1.png

If the indexes have names, you can use the level names instead of specifying the level numbers:

../_images/reshaping_unstack_0.png

Notice that the stack() and unstack() methods implicitly sort the index levels involved. Hence a call to stack() and then unstack() , or vice versa, will result in a sorted copy of the original DataFrame or Series :

The above code will raise a TypeError if the call to sort_index() is removed.

Multiple levels#

You may also stack or unstack more than one level at a time by passing a list of levels, in which case the end result is as if each level in the list were processed individually.

The list of levels can contain either level names or level numbers (but not a mixture of the two).

Missing data#

These functions are intelligent about handling missing data and do not expect each subgroup within the hierarchical index to have the same set of labels. They also can handle the index being unsorted (but you can make it sorted by calling sort_index() , of course). Here is a more complex example:

As mentioned above, stack() can be called with a level argument to select which level in the columns to stack:

Unstacking can result in missing values if subgroups do not have the same set of labels. By default, missing values will be replaced with the default fill value for that data type, NaN for float, NaT for datetimelike, etc. For integer types, by default data will converted to float and missing values will be set to NaN .

Alternatively, unstack takes an optional fill_value argument, for specifying the value of missing data.

With a MultiIndex#

Unstacking when the columns are a MultiIndex is also careful about doing the right thing:

Reshaping by melt#

../_images/reshaping_melt.png

The top-level melt() function and the corresponding DataFrame.melt() are useful to massage a DataFrame into a format where one or more columns are identifier variables, while all other columns, considered measured variables, are “unpivoted” to the row axis, leaving just two non-identifier columns, “variable” and “value”. The names of those columns can be customized by supplying the var_name and value_name parameters.

When transforming a DataFrame using melt() , the index will be ignored. The original index values can be kept around by setting the ignore_index parameter to False (default is True ). This will however duplicate them.

New in version 1.1.0.

Another way to transform is to use the wide_to_long() panel data convenience function. It is less flexible than melt() , but more user-friendly.

Combining with stats and GroupBy#

It should be no shock that combining pivot() / stack() / unstack() with GroupBy and the basic Series and DataFrame statistical functions can produce some very expressive and fast data manipulations.

Pivot tables#

While pivot() provides general purpose pivoting with various data types (strings, numerics, etc.), pandas also provides pivot_table() for pivoting with aggregation of numeric data.

The function pivot_table() can be used to create spreadsheet-style pivot tables. See the cookbook for some advanced strategies.

It takes a number of arguments:

data : a DataFrame object.

values : a column or a list of columns to aggregate.

index : a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values.

columns : a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values.

aggfunc : function to use for aggregation, defaulting to numpy.mean .

Consider a data set like this:

We can produce pivot tables from this data very easily:

The result object is a DataFrame having potentially hierarchical indexes on the rows and columns. If the values column name is not given, the pivot table will include all of the data in an additional level of hierarchy in the columns:

Also, you can use Grouper for index and columns keywords. For detail of Grouper , see Grouping with a Grouper specification .

You can render a nice output of the table omitting the missing values by calling to_string() if you wish:

Adding margins#

If you pass margins=True to pivot_table() , special All columns and rows will be added with partial group aggregates across the categories on the rows and columns:

Additionally, you can call DataFrame.stack() to display a pivoted DataFrame as having a multi-level index:

Cross tabulations#

Use crosstab() to compute a cross-tabulation of two (or more) factors. By default crosstab() computes a frequency table of the factors unless an array of values and an aggregation function are passed.

It takes a number of arguments

index : array-like, values to group by in the rows.

columns : array-like, values to group by in the columns.

values : array-like, optional, array of values to aggregate according to the factors.

aggfunc : function, optional, If no values array is passed, computes a frequency table.

rownames : sequence, default None , must match number of row arrays passed.

colnames : sequence, default None , if passed, must match number of column arrays passed.

margins : boolean, default False , Add row/column margins (subtotals)

normalize : boolean, <‘all’, ‘index’, ‘columns’>, or <0,1>, default False . Normalize by dividing all values by the sum of values.

Any Series passed will have their name attributes used unless row or column names for the cross-tabulation are specified

If crosstab() receives only two Series, it will provide a frequency table.

crosstab() can also be implemented to Categorical data.

If you want to include all of data categories even if the actual data does not contain any instances of a particular category, you should set dropna=False .

Normalization#

Frequency tables can also be normalized to show percentages rather than counts using the normalize argument:

normalize can also normalize values within each row or within each column:

crosstab() can also be passed a third Series and an aggregation function ( aggfunc ) that will be applied to the values of the third Series within each group defined by the first two Series :

Adding margins#

Finally, one can also add margins or normalize this output.

Tiling#

The cut() function computes groupings for the values of the input array and is often used to transform continuous variables to discrete or categorical variables:

If the bins keyword is an integer, then equal-width bins are formed. Alternatively we can specify custom bin-edges:

If the bins keyword is an IntervalIndex , then these will be used to bin the passed data.:

Computing indicator / dummy variables#

To convert a categorical variable into a “dummy” or “indicator” DataFrame , for example a column in a DataFrame (a Series ) which has k distinct values, can derive a DataFrame containing k columns of 1s and 0s using get_dummies() :

Sometimes it’s useful to prefix the column names, for example when merging the result with the original DataFrame :

This function is often used along with discretization functions like cut() :

get_dummies() also accepts a DataFrame . By default all categorical variables (categorical in the statistical sense, those with object or categorical dtype) are encoded as dummy variables.

All non-object columns are included untouched in the output. You can control the columns that are encoded with the columns keyword.

Notice that the B column is still included in the output, it just hasn’t been encoded. You can drop B before calling get_dummies if you don’t want to include it in the output.

As with the Series version, you can pass values for the prefix and prefix_sep . By default the column name is used as the prefix, and _ as the prefix separator. You can specify prefix and prefix_sep in 3 ways:

string: Use the same value for prefix or prefix_sep for each column to be encoded.

list: Must be the same length as the number of columns being encoded.

dict: Mapping column name to prefix.

Sometimes it will be useful to only keep k-1 levels of a categorical variable to avoid collinearity when feeding the result to statistical models. You can switch to this mode by turn on drop_first .

When a column contains only one level, it will be omitted in the result.

By default new columns will have np.uint8 dtype. To choose another dtype, use the dtype argument:

New in version 1.5.0.

To convert a “dummy” or “indicator” DataFrame , into a categorical DataFrame , for example k columns of a DataFrame containing 1s and 0s can derive a DataFrame which has k distinct values using from_dummies() :

Dummy coded data only requires k — 1 categories to be included, in this case the k th category is the default category, implied by not being assigned any of the other k — 1 categories, can be passed via default_category .

Factorizing values#

To encode 1-d values as an enumerated type use factorize() :

Note that factorize() is similar to numpy.unique , but differs in its handling of NaN:

The following numpy.unique will fail under Python 3 with a TypeError because of an ordering bug. See also here.

If you just want to handle one column as a categorical variable (like R’s factor), you can use df["cat_col"] = pd.Categorical(df["col"]) or df["cat_col"] = df["col"].astype("category") . For full docs on Categorical , see the Categorical introduction and the API documentation .

Examples#

In this section, we will review frequently asked questions and examples. The column names and relevant column values are named to correspond with how this DataFrame will be pivoted in the answers below.

Pivoting with single aggregations#

Suppose we wanted to pivot df such that the col values are columns, row values are the index, and the mean of val0 are the values? In particular, the resulting DataFrame should look like:

This solution uses pivot_table() . Also note that aggfunc=’mean’ is the default. It is included here to be explicit.

Note that we can also replace the missing values by using the fill_value parameter.

Also note that we can pass in other aggregation functions as well. For example, we can also pass in sum .

Another aggregation we can do is calculate the frequency in which the columns and rows occur together a.k.a. “cross tabulation”. To do this, we can pass size to the aggfunc parameter.

Pivoting with multiple aggregations#

We can also perform multiple aggregations. For example, to perform both a sum and mean , we can pass in a list to the aggfunc argument.

Note to aggregate over multiple value columns, we can pass in a list to the values parameter.

Note to subdivide over multiple columns we can pass in a list to the columns parameter.

Exploding a list-like column#

New in version 0.25.0.

Sometimes the values in a column are list-like.

We can ‘explode’ the values column, transforming each list-like to a separate row, by using explode() . This will replicate the index values from the original row:

You can also explode the column in the DataFrame .

Series.explode() will replace empty lists with np.nan and preserve scalar entries. The dtype of the resulting Series is always object .

Here is a typical usecase. You have comma separated strings in a column and want to expand this.

Creating a long form DataFrame is now straightforward using explode and chained operations

Pivot Tables

We have seen how the GroupBy abstraction lets us explore relationships within a dataset. A pivot table is a similar operation that is commonly seen in spreadsheets and other programs that operate on tabular data. The pivot table takes simple column-wise data as input, and groups the entries into a two-dimensional table that provides a multidimensional summarization of the data. The difference between pivot tables and GroupBy can sometimes cause confusion; it helps me to think of pivot tables as essentially a multidimensional version of GroupBy aggregation. That is, you split-apply-combine, but both the split and the combine happen across not a one-dimensional index, but across a two-dimensional grid.

Motivating Pivot Tables¶

For the examples in this section, we’ll use the database of passengers on the Titanic, available through the Seaborn library (see Visualization With Seaborn):

survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

This contains a wealth of information on each passenger of that ill-fated voyage, including gender, age, class, fare paid, and much more.

Pivot Tables by Hand¶

To start learning more about this data, we might begin by grouping according to gender, survival status, or some combination thereof. If you have read the previous section, you might be tempted to apply a GroupBy operation–for example, let’s look at survival rate by gender:

survived
sex
female 0.742038
male 0.188908

This immediately gives us some insight: overall, three of every four females on board survived, while only one in five males survived!

This is useful, but we might like to go one step deeper and look at survival by both sex and, say, class. Using the vocabulary of GroupBy , we might proceed using something like this: we group by class and gender, select survival, apply a mean aggregate, combine the resulting groups, and then unstack the hierarchical index to reveal the hidden multidimensionality. In code:

class First Second Third
sex
female 0.968085 0.921053 0.500000
male 0.368852 0.157407 0.135447

This gives us a better idea of how both gender and class affected survival, but the code is starting to look a bit garbled. While each step of this pipeline makes sense in light of the tools we’ve previously discussed, the long string of code is not particularly easy to read or use. This two-dimensional GroupBy is common enough that Pandas includes a convenience routine, pivot_table , which succinctly handles this type of multi-dimensional aggregation.

Pivot Table Syntax¶

Here is the equivalent to the preceding operation using the pivot_table method of DataFrame s:

class First Second Third
sex
female 0.968085 0.921053 0.500000
male 0.368852 0.157407 0.135447

This is eminently more readable than the groupby approach, and produces the same result. As you might expect of an early 20th-century transatlantic cruise, the survival gradient favors both women and higher classes. First-class women survived with near certainty (hi, Rose!), while only one in ten third-class men survived (sorry, Jack!).

Multi-level pivot tables¶

Just as in the GroupBy , the grouping in pivot tables can be specified with multiple levels, and via a number of options. For example, we might be interested in looking at age as a third dimension. We’ll bin the age using the pd.cut function:

class First Second Third
sex age
female (0, 18] 0.909091 1.000000 0.511628
(18, 80] 0.972973 0.900000 0.423729
male (0, 18] 0.800000 0.600000 0.215686
(18, 80] 0.375000 0.071429 0.133663

We can apply the same strategy when working with the columns as well; let’s add info on the fare paid using pd.qcut to automatically compute quantiles:

fare [0, 14.454] (14.454, 512.329]
class First Second Third First Second Third
sex age
female (0, 18] NaN 1.000000 0.714286 0.909091 1.000000 0.318182
(18, 80] NaN 0.880000 0.444444 0.972973 0.914286 0.391304
male (0, 18] NaN 0.000000 0.260870 0.800000 0.818182 0.178571
(18, 80] 0.0 0.098039 0.125000 0.391304 0.030303 0.192308

The result is a four-dimensional aggregation with hierarchical indices (see Hierarchical Indexing), shown in a grid demonstrating the relationship between the values.

Additional pivot table options¶

The full call signature of the pivot_table method of DataFrame s is as follows:

We’ve already seen examples of the first three arguments; here we’ll take a quick look at the remaining ones. Two of the options, fill_value and dropna , have to do with missing data and are fairly straightforward; we will not show examples of them here.

The aggfunc keyword controls what type of aggregation is applied, which is a mean by default. As in the GroupBy, the aggregation specification can be a string representing one of several common choices (e.g., ‘sum’ , ‘mean’ , ‘count’ , ‘min’ , ‘max’ , etc.) or a function that implements an aggregation (e.g., np.sum() , min() , sum() , etc.). Additionally, it can be specified as a dictionary mapping a column to any of the above desired options:

fare survived
class First Second Third First Second Third
sex
female 106.125798 21.970121 16.118810 91.0 70.0 72.0
male 67.226127 19.741782 12.661633 45.0 17.0 47.0

Notice also here that we’ve omitted the values keyword; when specifying a mapping for aggfunc , this is determined automatically.

At times it’s useful to compute totals along each grouping. This can be done via the margins keyword:

class First Second Third All
sex
female 0.968085 0.921053 0.500000 0.742038
male 0.368852 0.157407 0.135447 0.188908
All 0.629630 0.472826 0.242363 0.383838

Here this automatically gives us information about the class-agnostic survival rate by gender, the gender-agnostic survival rate by class, and the overall survival rate of 38%. The margin label can be specified with the margins_name keyword, which defaults to «All» .

Example: Birthrate Data¶

As a more interesting example, let’s take a look at the freely available data on births in the United States, provided by the Centers for Disease Control (CDC). This data can be found at https://raw.githubusercontent.com/jakevdp/data-CDCbirths/master/births.csv (this dataset has been analyzed rather extensively by Andrew Gelman and his group; see, for example, this blog post):

Taking a look at the data, we see that it’s relatively simple–it contains the number of births grouped by date and gender:

year month day gender births
0 1969 1 1 F 4046
1 1969 1 1 M 4440
2 1969 1 2 F 4454
3 1969 1 2 M 4548
4 1969 1 3 F 4548

We can start to understand this data a bit more by using a pivot table. Let’s add a decade column, and take a look at male and female births as a function of decade:

gender F M
decade
1960 1753634 1846572
1970 16263075 17121550
1980 18310351 19243452
1990 19479454 20420553
2000 18229309 19106428

We immediately see that male births outnumber female births in every decade. To see this trend a bit more clearly, we can use the built-in plotting tools in Pandas to visualize the total number of births by year (see Introduction to Matplotlib for a discussion of plotting with Matplotlib):

With a simple pivot table and plot() method, we can immediately see the annual trend in births by gender. By eye, it appears that over the past 50 years male births have outnumbered female births by around 5%.

Further data exploration¶

Though this doesn’t necessarily relate to the pivot table, there are a few more interesting features we can pull out of this dataset using the Pandas tools covered up to this point. We must start by cleaning the data a bit, removing outliers caused by mistyped dates (e.g., June 31st) or missing values (e.g., June 99th). One easy way to remove these all at once is to cut outliers; we’ll do this via a robust sigma-clipping operation:

This final line is a robust estimate of the sample mean, where the 0.74 comes from the interquartile range of a Gaussian distribution (You can learn more about sigma-clipping operations in a book I coauthored with Željko Ivezić, Andrew J. Connolly, and Alexander Gray: «Statistics, Data Mining, and Machine Learning in Astronomy» (Princeton University Press, 2014)).

With this we can use the query() method (discussed further in High-Performance Pandas: eval() and query() ) to filter-out rows with births outside these values:

Next we set the day column to integers; previously it had been a string because some columns in the dataset contained the value ‘null’ :

Finally, we can combine the day, month, and year to create a Date index (see Working with Time Series). This allows us to quickly compute the weekday corresponding to each row:

Using this we can plot births by weekday for several decades:

Apparently births are slightly less common on weekends than on weekdays! Note that the 1990s and 2000s are missing because the CDC data contains only the month of birth starting in 1989.

Another intersting view is to plot the mean number of births by the day of the year. Let’s first group the data by month and day separately:

The result is a multi-index over months and days. To make this easily plottable, let’s turn these months and days into a date by associating them with a dummy year variable (making sure to choose a leap year so February 29th is correctly handled!)

Focusing on the month and day only, we now have a time series reflecting the average number of births by date of the year. From this, we can use the plot method to plot the data. It reveals some interesting trends:

In particular, the striking feature of this graph is the dip in birthrate on US holidays (e.g., Independence Day, Labor Day, Thanksgiving, Christmas, New Year’s Day) although this likely reflects trends in scheduled/induced births rather than some deep psychosomatic effect on natural births. For more discussion on this trend, see the analysis and links in Andrew Gelman’s blog post on the subject. We’ll return to this figure in Example:-Effect-of-Holidays-on-US-Births, where we will use Matplotlib’s tools to annotate this plot.

Looking at this short example, you can see that many of the Python and Pandas tools we’ve seen to this point can be combined and used to gain insight from a variety of datasets. We will see some more sophisticated applications of these data manipulations in future sections!

© 2012-2017 Jake VanderPlas, license unless otherwise noted. Generated by Pelican.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *