Suppose I have a pandas DataFrame like this
name col1 col2 col3
0 AAA 1 0 2
1 BBB 2 1 2
2 CCC 0 0 2
I want (a) the names of any columns that contain a value of 2
anywhere in the column (i.e., col1
, col3
), and (b) the names of any columns that contain only values of 2
(i.e., col3
).
I understand how to use DataFrame.any()
and DataFrame.all()
to select rows in a DataFrame where a value appears in any or all columns, but I'm trying to find COLUMNS where a value appears in (a) any or (b) all rows.
CodePudding user response:
You can do what you described with columns:
df.columns[df.eq(2).any()]
# Index(['col1', 'col3'], dtype='object')
df.columns[df.eq(2).all()]
# Index(['col3'], dtype='object')
CodePudding user response:
You can loop over the columns (a):
for column in df.columns:
if (df[column]==2).any(axis=None)==True:
print(column "contains 2")
Here you get the name of the columns containing one or more 2.
(b) :
for column in df.columns:
if (df[column]==2).all(axis=None)==True:
print(column "contains 2")