1

My dataframe looks like:

A  B  C  D  ....  Y   Z
0  5  12 14       4   2
3  6  15 10       1   30
2  10 20 12       5   15

I want to create another dataframe that only contains the columns with an average value greater than 10:

C  D  .... Z
12 14      2
15 10      30
20 12      15

1 Answer 1

1

Use:

df = df.loc[:, df.mean() > 10]
print (df)
    C   D   Z
0  12  14   2
1  15  10  30
2  20  12  15

Detail:

print (df.mean())
A     1.666667
B     7.000000
C    15.666667
D    12.000000
Y     3.333333
Z    15.666667
dtype: float64

print (df.mean() > 10)
A    False
B    False
C     True
D     True
Y    False
Z     True
dtype: bool

Alternative:

print (df[df.columns[df.mean() > 10]])
    C   D   Z
0  12  14   2
1  15  10  30
2  20  12  15

Detail:

print (df.columns[df.mean() > 10])
Index(['C', 'D', 'Z'], dtype='object')
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, i tried it on my dataframe and got: IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match for some reason. I suppose there must be an issue with the dataframe itself I didnt notice
It seems you forget for loc. I add another solution, please chek it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.