Storing and manipulating data with pandas
pandas is an open-source toolkit built on top of NumPy that offers Python programmers high-performance, user-friendly data structures, and data analysis capabilities. It enables quick analysis, data preparation, and cleaning. It performs and produces at a high level.
pandas is a package for data analysis, and because it includes many built-in auxiliary functions, it is typically used for financial time series data, economic data, and any form of tabular data. For scientific computing, NumPy is a quick way to manage huge multidimensional arrays, and it can be used in conjunction with the SciPy and pandas packages.
Constructing a DataFrame from a dictionary is possible by passing this dictionary to the DataFrame
constructor:
import pandas as pd d = {'col1': [1,5,8, 2], 'col2': [3,3,7, 4]} df = pd.DataFrame(data=d) df
The pandas groupby
function is a powerful and versatile function that allows us to split data into separate groups to perform computations for better analysis:
df = pd.DataFrame({'Animal': ['Dog', 'Dog', 'Rat', 'Rat','Rat'], 'Max Speed': [380., 370., 24., 26.,25.], 'Max Weight': [10., 8.1, .1, .12,.09]}) df
The three steps of “split,” “apply,” and “combine” make it the simplest to recall what a “groupby” performs. Split refers to dividing your data into distinct groups based on a particular column. As an illustration, we can divide our sales data into months:
df.groupby(['Animal']).mean()
pandas’ groupby
technique is extremely potent. Using value counts, you can group by one column and count the values of a different column as a function of this column value. We can count the number of activities each person completed using groupby
and value
counts:
df.value_counts()
We can also aggregate data over the rows using the aggregate()
method, which allows you to apply a function or a list of function names to be executed along one of the axes of the DataFrame. The default is 0, which is the index (row) axis. It’s important to note that the agg()
method is an alias of the aggregate()
method:
df.agg("mean", axis="rows",numeric_only=True)
We can also pass several functions to be used in each of the selected columns:
df.agg({'Max Speed' : ['sum', 'min'], 'Max Weight' : ['mean', 'max']})
The quantile of the values on a given axis is determined via the quantile()
method. The row-level axis is the default. The quantile()
method calculates the quantile column-wise and returns the mean value for each row when the column axis is specified (axis='columns'
). The following line will give us the 10% quantile across the entire DataFrame:
df.quantile(.1)
We can also pass a list of quantiles:
df.quantile([.1, .5])
The pivot()
function is used to reshape a given DataFrame structured by supplied index or column values and is one of the different types of functions that we can use to change the data. Data aggregation is not supported by this function; multiple values produce a MultiIndex
in the columns:
df = pd.DataFrame( {'type': ['one', 'one', 'one', 'two', 'two', 'two'], 'cat': ['A', 'B', 'C', 'A', 'B', 'C'], 'val': [1, 2, 3, 4, 5, 6], 'letter': ['x', 'y', 'z', 'q', 'w', 't']}) df.pivot(index='type', columns='cat', values='val')
Pivot tables are one of pandas’ most powerful features. A pivot table allows us to draw insights from data. pandas provides a similar function called pivot_table()
. It is a simple function but can produce a very powerful analysis very quickly.
The next step for us will be to learn how to visualize the data to create proper storytelling and appropriate interpretations.