In this article, we will learn to find the mean of dataframe in pandas. Pandas provide a method mean() that returns a mean of the requested axis. We can find the mean column-wise and row-wise of dataframe. Let's see some examples.
This is the syntax of the mean method in pandas.
DataFrame.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
In this example, we are finding the mean of dataframe by using the mean() method.
# import pandas and numpy
import pandas as pd
import numpy as np
numpy_data = np.array([[10, 20], [30, 40], [50, 60]]) # Create array using numpy array
df = pd.DataFrame(data=numpy_data, index=[1, 2, 3], columns=["Column A", "Column B"])
# print dataframe
print(df)
# print dataframe mean
print(df.mean())
Output:
Column A Column B
1 10 20
2 30 40
3 50 60
Column A 30.0
Column B 40.0
dtype: float64
In this example, we are finding the mean of dataframe row-wise by using the mean() method.
# import pandas and numpy
import pandas as pd
import numpy as np
numpy_data = np.array([[10, 20], [30, 40], [50, 60]]) # Create array using numpy array
df = pd.DataFrame(data=numpy_data, index=[1, 2, 3], columns=["Column A", "Column B"])
# print dataframe
print(df)
# print dataframe mean
print(df.mean(axis=1))
Output:
Column A Column B
1 10 20
2 30 40
3 50 60
1 15.0
2 35.0
3 55.0
dtype: float64
In this example, we are handling null values of a dataframe while finding the mean of the dataframe. The mean() method accepts an argument skipna to skip the null value while calculating the mean. See the example below.
# import pandas and numpy
import pandas as pd
import numpy as np
numpy_data = np.array([[10, 20], [30, 40], [50, 60]]) # Create array using numpy array
df = pd.DataFrame(data=numpy_data, index=[1, 2, 3], columns=["Column A", "Column B"])
# print dataframe
print(df)
# print dataframe mean
print(df.mean(axis=1, skipna=True))
Output:
Column A Column B
1 10 20
2 30 40
3 50 60
1 15.0
2 35.0
3 55.0
dtype: float64
In this example, we are finding the mean of the specified column of dataframe. By default, the mean() method returns the mean of all rows or columns but if you want to get the mean of a single column then use the examples.
# import pandas and numpy
import pandas as pd
import numpy as np
numpy_data = np.array([[10, 20], [30, 40], [50, 60]]) # Create array using numpy array
df = pd.DataFrame(data=numpy_data, index=[1, 2, 3], columns=["Column A", "Column B"])
# print dataframe
print(df)
# print dataframe mean
print(df["Column A"].mean())
Output:
Column A Column B
1 10 20
2 30 40
3 50 60
30.0
In this example, we are using describe() method to find mean, count, std, min, max, etc of dataframe. Use this single method when you need multiple result in a single statement.
# import pandas and numpy
import pandas as pd
import numpy as np
numpy_data = np.array([[10, 20], [30, 40], [50, 60]]) # Create array using numpy array
df = pd.DataFrame(data=numpy_data, index=[1, 2, 3], columns=["Column A", "Column B"])
# print dataframe
print(df)
# print dataframe mean
print(df.mean(axis=1))
print(df.describe())
Output:
Column A Column B
1 10 20
2 30 40
3 50 60
1 15.0
2 35.0
3 55.0
dtype: float64
Column A Column B
count 3.0 3.0
mean 30.0 40.0
std 20.0 20.0
min 10.0 20.0
25% 20.0 30.0
50% 30.0 40.0
75% 40.0 50.0
max 50.0 60.0