To calculate the average across columns in pandas, you can use the mean method on a DataFrame object. The mean method returns the mean of the values over the requested axis. By default, the axis is 0, which means the mean is calculated along the index (row) axis. If you want to calculate the mean along the column axis, you can specify axis=1 as an argument.
Average Across Columns
You can calculate the average across columns for each row by using:
df.mean(axis=1)
You can also select specific columns to calculate the mean by using:
df[['age', 'weight']].mean(axis=1)
Column Mean Including NaN Values
To get the column mean including NaN values in pandas, you can use the describe method on a DataFrame object. The describe method returns a summary of the descriptive statistics for each column, such as count, mean, std, min, max, and percentiles. By default, the describe method excludes NaN values from the calculation, but you can include them by setting the skipna argument to False.
You can calculate the column mean including NaN values by using:
df.describe(skipna=False)
You can see that the mean values are different from the ones that exclude NaN values. You can also select specific columns to calculate the mean by using:
df[['age', 'weight']].describe(skipna=False)
See more:
Pandas documentation
describe documentation