[Solved] select variable from column in pandas


This will give you filtered dataframe with all the columns where Region is Europe and Purchased Bike is Yes

agestock = pd.DataFrame({
  'Region': {0: 'Europe', 1: 'Europe', 2: 'Europe', 3: 'APAC', 4: 'US'},  
  'Age': {0: 36, 1: 43, 2: 48, 3: 33, 4: 43},  
  'Purchased Bike': {0: 'Yes', 1: 'Yes', 2: 'Yes', 3: 'No', 4: 'Yes'}})

In [2]: agestock
Out[2]:
   Region   Age Purchased Bike
0  Europe    36            Yes
1  Europe    43            Yes
2  Europe    48            Yes
3    APAC    33             No
4      US    43            Yes

In [3]: agestock.query('Region == "Europe" and `Purchased Bike` == "Yes"')
Out[3]:
   Region   Age Purchased Bike
0  Europe    36            Yes
1  Europe    43            Yes
2  Europe    48            Yes

Alternative approach

In [4]: filter = agestock['Purchased Bike'] == 'Yes'


In [5]: agestock[filter]
Out[5]:
   Region   Age Purchased Bike
0  Europe    36            Yes
1  Europe    43            Yes
2  Europe    48            Yes
4      US    43            Yes

In [6]: agestock[filter].Region
Out[6]:
0    Europe
1    Europe
2    Europe
4        US
Name: Region, dtype: object

4

solved select variable from column in pandas