pandas select columns where conditions code example
Example 1: 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']]
Example 2: make a condition statement on column pandas
df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
Example 3: how to slicing dataframe using two conditions
movies[(movies.duration >= 200) & (movies.genre == 'Drama')]
Example 4: 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
Example 5: make a condition statement on column pandas
df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'