Answers for "pandas select rows by multiple conditions"

4

how to slicing dataframe using two conditions

# when you wrap conditions in parantheses, you give order
# you do those in brackets first before 'and'
# AND
movies[(movies.duration >= 200) & (movies.genre == 'Drama')]
Posted by: Guest on March-21-2020
6

select rows with multiple conditions pandas query

df.loc[(df['Salary_in_1000']>=100) & (df['Age']< 60) & (df['FT_Team'].str.startswith('S')),['Name','FT_Team']]
Posted by: Guest on June-20-2020
2

select rows with multiple conditions pandas query

df.query('Salary_in_1000 >= 100 & Age < 60 & FT_Team.str.startswith("S").values')
Posted by: Guest on June-20-2020
2

pandas select rows by multiple conditions

>>> df["A"][(df["B"] > 50) & (df["C"] == 900)]
2    5
3    8
Name: A, dtype: int64
    
>>> df.loc[(df["B"] > 50) & (df["C"] == 900), "A"]
2    5
3    8
Name: A, dtype: int64
>>> df.loc[(df["B"] > 50) & (df["C"] == 900), "A"].values
array([5, 8], dtype=int64)
>>> df.loc[(df["B"] > 50) & (df["C"] == 900), "A"] *= 1000
>>> df
      A   B    C
0     9  40  300
1     9  70  700
2  5000  70  900
3  8000  80  900
4     7  50  900
Posted by: Guest on March-30-2020
3

new dataframe based on certain row conditions

# Create variable with TRUE if nationality is USA
american = df['nationality'] == "USA"

# Create variable with TRUE if age is greater than 50
elderly = df['age'] > 50

# Select all cases where nationality is USA and age is greater than 50
df[american & elderly]
Posted by: Guest on March-02-2020
0

select rows with multiple conditions pandas query

df.loc[idx]
Posted by: Guest on June-20-2020

Code answers related to "pandas select rows by multiple conditions"

Python Answers by Framework

Browse Popular Code Answers by Language