Haraball.no

Find all rows and columns in a pandas DataFrame which fit a clause

2016-11-18

data = {
    'A': [3, 3, 5, 3],
    'B': [7, 2, 9, 2],
    'C': [3, 3, 3, 0],
    'D': [4, 4, 4, 1],
    'E': [9, 2, 5, 0]
}

df = pd.DataFrame(data)

print(df)

#    A  B  C  D  E
# 0  3  7  3  4  9
# 1  3  2  3  4  2
# 2  5  9  3  4  5
# 3  3  2  0  1  0

# We only want the cells with values more than 2
df > 2

#       A      B      C      D      E
# 0  True   True   True   True   True
# 1  True  False   True   True  False
# 2  True   True   True   True   True
# 3  True  False  False  False  False

Here there are True values for rows with index 0 and 2. How can get these be found?

Assign the clause to a new DataFrame:

df2 = df > 2

df2.all(axis=1)

# 0     True
# 1    False
# 2     True
# 3    False

With axis=1 we look at the rows.

Use axis=0 to look at the colums. Now the clause >2 is True for the first column (A):

df2.all(axis=0)

# A     True
# B    False
# C    False
# D    False
# E    False