1 | initial version |
To search a Pandas dataframe for values in a specific column that meet multiple conditions on another column being True, you can use the following code:
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'col1': ['A', 'B', 'C', 'D', 'E'],
'col2': [10, 20, 30, 40, 50],
'col3': [True, False, True, True, False]})
# search for values in col1 where col3 is True and col2 is greater than 20
result = df.loc[(df['col3'] == True) & (df['col2'] > 20), 'col1']
print(result)
Output:
2 C
3 D
Name: col1, dtype: object
In this example, we first create a sample dataframe with three columns. We then use the loc
method to filter the dataframe by selecting rows where col3
is True
and col2
is greater than 20
. Finally, we select the values in col1
that meet both conditions. The resulting output is the values C
and D
in col1
.