Reputation: 361
I have below dataframe in Pandas. It stored in df that works similar to this:
+--------+--------+-------+
| Col1 | Col2 | Col3 |
+--------+--------+-------+
| Team 1 | High | Pizza |
| Team 2 | Medium | Sauce |
| Team 2 | Low | Crust |
+--------+--------+-------+
I need Col3
value, which have Col1=Team 2
and Col2=Medium
.
I tried below code for this:
result = df.loc[(df[Col1 ]=='Team 2) ' AND (df[Col2 ]=='Medium'), 'Col3'].values[0]
print(result)
Expected result:
'Sauce'
But I getting error. Please suggest
Upvotes: 1
Views: 964
Reputation: 862511
Use &
for bitwise AND
- solution working if always data matched:
result = df.loc[(df['Col1'] =='Team2') & (df['Col2']=='Medium'), 'Col3'].values[0]
Another solution with next
and iter
for return default value, if no match data:
s = df.loc[(df['Col1'] =='Team2') & (df['Col2']=='Medium'), 'Col3']
result = next(iter(s, 'no matching')
Upvotes: 2