# Explore Pandas Plotting: Visualizing Data Distribution in DataFrames

## Plotting Distributions in Pandas DataFrames

Are you curious about how to visualize the distribution of data in your Pandas DataFrame? Look no further! In this article, we will cover two methods to plot distributions of values in one column or grouped by another column.

### Method 1: Plot Distribution of Values in One Column

The first method involves plotting the distribution of values within a single column of your DataFrame. This can be accomplished using two different types of plots: kernel density estimation (KDE) or a histogram.

KDE plots provide a smooth estimate of the underlying distribution of data, while histograms break the data into discrete bins and display the frequency of each bin.

### Using Method 1 to Plot Distribution of Points Column

Suppose that you have a DataFrame of NBA statistics for the 2020-21 season, and you are interested in visualizing the distribution of points scored by players. Using the Pandas plot function, you can create a KDE plot of the points column with the following code:

``````import pandas as pd
import matplotlib.pyplot as plt

nba_df["points"].plot(kind='kde')

plt.show()
``````

The resulting plot will show the estimated distribution of points scored by NBA players. You can adjust the bandwidth of the KDE plot by using the bw_method parameter.

By default, the function uses Scott’s rule to determine the bandwidth, but you can also specify your own bandwidth estimator.

### Using Method 1 to Plot Histogram of Points Column

Alternatively, you can plot a histogram of the points column using the following code:

``````nba_df["points"].plot(kind='hist', bins=20)

plt.show()
``````

In this example, the histogram is divided into 20 equally spaced bins, with the height of each bin representing the frequency of points scored within that bin. You can adjust the number of bins to make the histogram more or less granular.

Additionally, you can specify whether you want the histogram to display absolute frequencies or normalized frequencies by using the density parameter.

### Method 2: Plot Distribution of Values in One Column, Grouped by Another Column

If you want to compare the distribution of values in a column across different groups within your DataFrame, you can use the groupby function to split your DataFrame into subgroups based on a categorical variable.

You can then plot the distributions of values within each subgroup using either KDE plots or histograms.

### Using Method 2 to Plot Grouped Distribution of Points Column

Continuing with the example of NBA statistics, suppose that you want to compare the distribution of points scored by players on different teams. You can group your DataFrame by the team column and plot a KDE or histogram for each team using the following code:

``````nba_df.groupby(by="team")["points"].plot(kind='kde')

plt.show()

nba_df.groupby(by="team")["points"].plot(kind='hist', bins=20, alpha=0.5, density=True)

plt.show()
``````

In the first line of code, the groupby function splits the DataFrame into subgroups based on the unique values in the team column, and the plot function creates a KDE plot of the points column for each subgroup.

The resulting plot shows the estimated distribution of points scored by players on each team. In the second line of code, the groupby function is used again to split the DataFrame into subgroups, and the plot function creates a histogram of the points column for each subgroup.

The alpha parameter is set to 0.5 to make the bars semi-transparent, and the density parameter is set to True to display normalized frequencies instead of absolute frequencies. The resulting plot shows the relative frequency of different point totals for each team, allowing for easy visual comparison.

## Conclusion

In conclusion, Pandas provides a powerful set of tools for visualizing the distribution of data in DataFrames. Using the plot function and groupby function, you can easily create KDE plots and histograms of data in one column or grouped by another column.

By choosing the appropriate plot and adjusting the parameters to suit your needs, you can gain insight into the distribution of data in your DataFrame and make informed decisions based on the patterns that you observe.

### Example 2: Plotting Distribution of Values in One Column, Grouped by Another Column

As referenced in the previous example, Method 2 of plotting distributions in Pandas involves using groupby function to split your DataFrame into subgroups based on a categorical variable.

This is particularly useful when you want to compare the distribution of values in a column across different groups within your DataFrame.

### Using Method 2 to Plot Distribution of Points Column, Grouped by Team Column

Continuing with the NBA statistics example, let’s explore how we can use groupby and the plot function to visualize the distribution of points scored by players on different teams.

The first step will be to group the DataFrame by the team column.

``````team_groups = nba_df.groupby("team")
``````

We can then iterate over the groups to plot a KDE of the points scored for each team.

``````for team, group in team_groups:
group["points"].plot(kind='kde', label=team)

plt.legend()
plt.show()
``````

In this example, we iterate over the groups created by the team column using a for loop. For each team, we extract the points column and plot a KDE of the values.

We also add a label for each team to the plot and finally display a legend. Alternatively, we can plot histograms of the points scored for each team using the same approach as we did in Subtopic 2.2.

``````for team, group in team_groups:
group["points"].plot(kind='hist', bins=20, alpha=0.5, density=True, label=team)

plt.legend()
plt.show()
``````

In this example, we again iterate over the groups created by the team column using a for loop.

For each team, we extract the points column and plot a histogram of the values. We also add a label for each team to the plot and display a legend.

Using the groupby function to split data into subgroups based on a categorical variable allows for easy comparison of the distribution of values in a column across different groups. By iterating over the groups, we can create a plot for each subgroup, labeling each group accordingly, and displaying a legend to make it easier to differentiate between the subgroups.

Pandas offers various tutorials and resources to help you work with DataFrames, such as data manipulation and data analysis techniques. Here are some useful resources to get started:

### Common Tasks in Pandas

If you are new to Pandas, you may find it helpful to start with some common tasks in the library. The Pandas documentation provides a useful introduction to basic data analysis techniques such as data manipulation and cleaning.

These tasks include renaming columns, dealing with missing or duplicate data, and subsetting data based on certain conditions. Pandas also offers tutorials and examples that cover more advanced topics, such as merging and joining data from multiple DataFrames, working with time-series data, and applying statistical functions to data.

In addition to the official documentation, there are numerous online resources available for learning Pandas. Websites such as Kaggle, GitHub, and Stack Overflow provide a wealth of online tutorials and examples of how to perform data manipulation and analysis using Pandas.

## Conclusion

This article has presented two methods for plotting distributions in Pandas DataFrames, allowing for visualization of distributions of data based on a column or across groups created by a categorical column. The use of groupby function simplifies the subsetting of data and creation of plots for each subgroup.

There are several online resources available to improve your understanding and proficiency with Pandas, which is useful for performing data analysis and data manipulation tasks. In conclusion, this article has covered two methods for plotting distributions in Pandas DataFrames, focusing on distributions based on a single column or across groups created by a categorical column.

The use of groupby function has proved essential for subsetting data and creating plots for each subgroup. With data manipulation and exploration tasks being a crucial part of data analysis, this article highlights the importance of having a firm grasp of Pandas functionalities and offers resources for developing and improving these skills.

Ultimately, constructing and analyzing plots of data distributions is critical to making informed decisions based on patterns and information derived from the data. It is important for data analysts to be proficient in the use of data science tools, such as Pandas, to effectively manage and derive insights from data.