# Mastering Data Analysis in Pandas: Mean Median and Mode

Data analysis is an essential skill for anyone working with data. In particular, analyzing data in Pandas can be an efficient way to manage and manipulate large datasets.

One important aspect of data analysis is calculating the mean, median, and mode of numerical data. In this article, we’ll look at the functions available in Pandas for calculating these statistical measures and how they are applied in the context of basketball player data.

## Data Analysis in Pandas

Pandas is a Python library specifically developed for data manipulation and analysis. It provides features for handling different types of data such as DataFrame, Series and Panel, and is particularly useful in handling large datasets.

Pandas provides functions for many statistical measures, including mean, median, and mode. The mean of a set of numerical data is the average value.

It is calculated by adding up all the values in the dataset and dividing by the number of observations. Pandas provides the function “mean()” that calculates the mean of each column in a DataFrame.

This function can be used to quickly calculate the average value of specific numerical data. The median is the middle value in a sorted dataset, with an equal number of values above and below it.

While not as commonly used as the mean, it can be useful in certain cases. The function “median()” in Pandas calculates the median for each column in a DataFrame.

The mode is the value that appears most frequently in a dataset. The mode can be useful for examining the most common occurrence of a value within a dataset.

Pandas provides the “mode()” function to calculate the mode for each column in a DataFrame. Example of calculating mean, median, and mode for basketball player data

Let’s apply these statistical functions to basketball player data.

We will use a dataset that includes information for basketball players for a number of games. The data includes each player’s points per game, rebounds per game, and minutes per game.

To calculate the mean, median, and mode of the dataset, we can use the following syntax:

``````import pandas as pd
# calculate mean
mean_scores = data.mean()
print("Mean scores: n", mean_scores)
# calculate median
median_scores = data.median()
print("Median scores: n", median_scores)
# calculate mode
mode_scores = data.mode()
print("Mode scores: n", mode_scores)
``````

The output will display the calculated mean, median, and mode for each column in the dataset, as shown below:

``````Mean scores:
PPG       10.80
RPG        4.14
MPG       17.04
dtype: float64
Median scores:
PPG       10.5
RPG        3.7
MPG       17.5
dtype: float64
Mode scores:
PPG  RPG   MPG
0  8.0  3.5  16.0
``````

From the output, we can see that the mean number of points per game is 10.80, the median number of points per game is 10.5, and the mode number of points per game is 8.

## Mean Calculation in Pandas

The “mean()” function in Pandas calculates the mean of each column in a DataFrame. However, it is important to note that this function will only work on columns with numerical data.

It will ignore any strings or non-numerical data. Here is an example of calculating the mean of specific columns in a dataset using Pandas:

``````import pandas as pd
# calculate mean of points per game
mean_points = data['PPG'].mean()
print("Mean points per game: ", mean_points)
# calculate mean of minutes per game
mean_minutes = data['MPG'].mean()
print("Mean minutes per game: ", mean_minutes)
``````

The output will display the mean value for each specific column, as shown below:

``````Mean points per game:  10.8
Mean minutes per game:  17.04
``````

## Output examples for mean value calculation

In addition to displaying the calculated mean result, we can use the functions “describe()” and “info()” to provide additional information about the data. The “describe()” function provides statistical information on each column, such as the count, mean, standard deviation, minimum value, and maximum value.

### Here is an example of using Pandas to calculate the mean of each column and provide a statistical summary of the data:

``````import pandas as pd
# calculate mean
mean_scores = data.mean()
print("Mean scores: n", mean_scores)
print("nSummary statistics:")
print(data.describe())
``````

The output will display the mean value for each column as well as statistical information on each column, as shown below:

``````Mean scores:
PPG       10.80
RPG        4.14
MPG       17.04
dtype: float64
Summary statistics:
PPG         RPG        MPG
count  10.000000   10.000000  10.000000
mean   10.800000    4.140000  17.040000
std     3.371396    1.845898   3.400396
min     5.000000    1.700000  12.000000
25%     9.125000    3.175000  15.925000
50%    10.500000    3.700000  17.500000
75%    12.750000    5.075000  20.225000
max    15.000000    7.700000  22.000000
``````

## Conclusion

In this article, we have discussed the basics of data analysis in Pandas, specifically focusing on calculating the mean, median, and mode of numeric data. These functions are crucial for understanding and interpreting numerical data, and can be used in a variety of different contexts.

By following the examples provided, you should be able to begin working with these functions yourself and conducting your own data analysis in Pandas.

## 3) Median Calculation in Pandas

In statistics, the median is the middle value in a dataset when the data is arranged in ascending or descending order. It is a statistical measure used to represent the midpoint value of a set of data, which avoids issues with outliers that can affect the accuracy of the mean.

In Pandas, the “median()” function can be used to calculate the median of each column in a DataFrame.

### Syntax for calculating median of numeric columns in a DataFrame

To calculate the median value of numeric columns in a DataFrame, we can use the “median()” function in Pandas. The syntax for using this function is as follows:

``````import pandas as pd
median_values = data.median()
print("Median values: n", median_values)
``````

In this example, we first import the Pandas library and then read in a CSV file containing our data. We then use the “median()” function to calculate the median value of each column in the DataFrame.

Finally, we use the “print()” function to display the median values calculated.

### Output examples for median value calculation

The median value calculated by the “median()” function is an important summary statistic that helps us understand the central tendency of our data. In combination with other statistics such as mean and standard deviation, the median can provide a more accurate representation of the distribution of our data.

### Here is an example of using Pandas to calculate the median of each column in a dataset:

``````import pandas as pd
# calculate median
median_data = data.median()
# display output
print("Median Values of the Data:n", median_data)
``````

### The output for the above code block will be as follows:

``````Median Values of the Data:
A    25.5
B    24.5
C    15.5
D    25.0
dtype: float64
``````

Here we can see that the median value of column A is 25.5, the median value of column B is 24.5, the median value of column C is 15.5, and the median value of column D is 25.0.

## 4) Mode Calculation in Pandas

The mode is a statistical measure that represents the most commonly occurring value in a dataset. The mode is the value that appears most frequently in a set of data, making it an essential tool in understanding the underlying distribution of the data.

Pandas provides the “mode()” function to calculate the mode of each column in a DataFrame.

### Syntax for calculating mode of numeric columns in a DataFrame

To calculate the mode of numeric columns in a DataFrame, we can use the “mode()” function in Pandas. The syntax for using this function is as follows:

``````import pandas as pd
mode_values = data.mode()
print("Mode values: n", mode_values)
``````

In this example, we first import the Pandas library and then read in a CSV file containing our data. We then use the “mode()” function to calculate the mode of each column in the DataFrame.

Finally, we use the “print()” function to display the mode values calculated.

### Output examples for mode value calculation

Like median and mean, the mode can provide important information about the central tendency of our data. By calculating the mode of our data, we can identify the most frequently occurring values or patterns in our dataset, which can be useful in understanding and predicting future trends.

### Here is an example of using Pandas to calculate the mode of each column in a dataset:

``````import pandas as pd
# calculate mode
mode_data = data.mode()
# display output
print("Mode Values of the Data:n", mode_data)
``````

### The output for the above code block will be as follows:

``````Mode Values of the Data:
A   B  C   D
0  23  10  2  14
1  24  23  6  25
``````

Here we can see that the mode value of column A is either 23 or 24, the mode value of column B is either 10 or 23, the mode value of column C is either 2 or 6, and the mode value of column D is either 14 or 25. Since there can be multiple modes in a dataset, Pandas displays all possible modes in the output as a DataFrame.

## Conclusion

Data analysis is a critical skill that can help uncover valuable insights and make informed decisions. In this article, we explored the syntax and output examples for calculating the median and mode of numeric columns in a DataFrame using Pandas.

Understanding these statistical measures can help us gain a deeper understanding of the underlying distribution of our data, and can be used to identify trends and patterns that may be hidden within the data. By using the examples and syntax provided in this article, you can begin to apply these tools in your own data analysis projects.

## 5) Additional Resources for Pandas

Pandas is a versatile library that provides a wide range of functions for manipulating and analyzing data in Python. In addition to calculating mean, median, and mode, there are many other commonly used operations that can be performed using Pandas.

### Explanation of other common operations in Pandas

1. Handling Missing Data – Missing data is common in real-world datasets.
2. Pandas provides functions for identifying and handling missing data, such as the “isna()” and “dropna()” functions. 2.
3. Grouping Data – Grouping data is a powerful operation that allows you to create subsets of your data based on one or more criteria. Pandas provides the “groupby()” function for grouping data based on specific columns.
4. Merging and Joining Data – Often, data is split across multiple files or tables.
5. Pandas provides functions such as “merge()” and “join()” to combine data from multiple sources. 4.
6. Reshaping Data – Sometimes you may need to reshape your data to better fit your analysis. Pandas provides functions for pivoting data (for example, converting row data to column data) and “melting” data (for example, combining multiple columns into one).
7. Applying Functions to Data – Often, you may need to apply a custom function to your data.
8. Pandas provides the “apply()” function for applying a given function to each element in a DataFrame. 6.
9. Working with Time Series Data – Pandas has extensive capabilities for working with time series data. This includes functions for handling dates and times and for creating time-based subsets of data.

There are a variety of resources available for learning more about Pandas. The official Pandas documentation is an excellent place to start.

It provides detailed documentation on all of the functions and features of the library, as well as numerous examples and tutorials. For those new to Pandas, there are many online tutorials available.

### Some popular options include:

1. Pandas Documentation – The official documentation provides a wide range of tutorials and examples.
2. DataCamp – DataCamp provides a comprehensive Pandas course that covers everything from simple data operations to more advanced data wrangling.
3. Kaggle – Kaggle offers a variety of Pandas tutorials and notebooks, as well as datasets to practice with.
4. RealPython – RealPython provides a beginner-friendly introduction to Pandas, with step-by-step instructions and clear examples.

Some popular channels include Corey Schafer and Keith Galli. In addition to these resources, Pandas has a large and active community, with many forums and discussion groups available for asking questions and seeking help.

Some popular options include the Pandas Google Group and the Stack Overflow Pandas tag.

## Conclusion

Pandas is a powerful and versatile library that provides numerous functions for manipulating and analyzing data in Python. In addition to the basic statistical measures such as mean, median, and mode, there are many other common operations that can be performed using Pandas.

By exploring the tutorials and resources available and experimenting with different operations, you can become proficient in working with Pandas and find valuable insights in your data. In summary, this article explored the basics of data analysis in Pandas, focusing on calculating the mean, median, and mode of numeric data.

We also covered additional common operations in Pandas, such as handling missing data, merging and joining data, grouping data, and reshaping data. Finally, we provided additional resources and tutorials for learning more about working with Pandas.

It is important to understand these statistical measures and common operations in order to gain a deeper understanding of the underlying distribution of data and uncover valuable insights. By following the examples and utilizing the resources provided, readers can become proficient in working with Pandas and improve their data analysis skills.