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
.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-21 01:03:08 +0000
Seen: 11 times
Last updated: May 21 '23
How do you update a dataframe within a for loop in R after passing a list?
How can we bring googlesheets data into a pyspark dataframe?
How can a portion of a file name be retrieved and stored in a DataFrame using Pandas?
How can we perform aggregate functions on particular datetime values in a Pandas DataFrame?
How can you display a Pandas Dataframe using a for loop?
What is the method for computing the overall sum of a dataframe, excluding a singular row?
How can the list within a dataframe be transformed so that it becomes a binary data type?
What is the method for making a struct in a Spark dataframe less complex?