Pandas's DataFrame offers powerful methods and idioms for data manipulation. Here's an example of how to select values based on complex criteria:
Consider a DataFrame with columns "A," "B," and "C". Select values from "A" for which corresponding values for "B" are greater than 50 and for "C" are not equal to 900.
import pandas as pd from random import randint df = pd.DataFrame({'A': [randint(1, 9) for x in range(10)], 'B': [randint(1, 9)*10 for x in range(10)], 'C': [randint(1, 9)*100 for x in range(10)]})
b_criteria = df["B"] > 50 c_criteria = df["C"] != 900
selection_criteria = b_criteria & c_criteria
selected_rows = df.loc[selection_criteria, "A"]
print(selected_rows) # Output: # 2 5000 # 3 8000 # Name: A, dtype: int64
Note:
Using .loc ensures that modifications made to the selected data only affect a copy, preserving the original DataFrame's integrity.
The above is the detailed content of How to Select Data from a Pandas DataFrame Based on Multiple Conditions?. For more information, please follow other related articles on the PHP Chinese website!