Alex Colombari
Alex Colombari

Reputation: 159

Mean of each column in dataframe

Working with a dataframe that contain an specific binary column (all in numpy array), in example:

[1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 1. 1. 0. 1. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 1. 1. 0. 0. 0. 0.]
[1. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 0.]
[1. 0. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 0.]
[1. 0. 0. 1. 0. 0. 0. 1. 0. 0. 1. 0. 0.]
[1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 1. 1. 0. 0. 0. 0.]

That's possible to apply a loop for iterate all columns inside that column?

i.e

In my case i need to get the mean of values in each column, like:

# Mean of position [0]           # Mean of position[3]
    1.                               1.
    1.                               0.
    1.                               0.
    0.                               1.
    1.                               1.
    0.                               1.
    1.                               0.
    1.                               1.
    1.                               1.
    0.                               0.

There is any way to do that?

Thanks!

Upvotes: 1

Views: 1173

Answers (2)

Dionys
Dionys

Reputation: 3133

You can use the mean function from numpy. https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html

So in your case, I think you are looking for np.mean(a, axis=1)

Upvotes: 1

Code Pope
Code Pope

Reputation: 5459

Just use iloc with mean:

meanZero = df.iloc[0].mean()
meanThird= df.iloc[3].mean()

Upvotes: 2

Related Questions