Taylor Schmac
Taylor Schmac

Reputation: 1

Selecting random row from dataframe in python based on certain conditions

Hi this is probably a very basic fix but I am just completely stuck and don't know enough about Python to figure out how to go about this myself. I made a dictionary of restaurants in my city and created a data frame of them. The whole program is just supposed to pick a random restaurant out of the dataframe. However, I want it to be able to select random restaurants based on certain things. For instance, "Cuisine" is a category and I want it to be able to select a random restaurant(row) based on cuisine being Mexican. I hope that makes sense because I am very lost.

my code is also below but there is not much to it

import pandas as pd


# Define a dictionary containing employee data 
data = {'Restaurant':['August Henrys Burger Bar', 'Bridges & Bourbon', 'The Capital Grille', 'Chinatown Inn', 'Chipotle','Condado Tacos','Crafted North','Cristos Mediterranean Grille','Five Guys','Forbes Tavern','Freshii','Genoa Pizza & Bar','Giovannis Pizza & Pasta','Hello Bistro','Joe and Pie Cafe & Pizzeria','Las Velas','Mandarin Gourmet','McCormick and Schmick','Moes Southwest Grill','Nickys Thai Kitchen','Noodles & Company','The Original Oyster House','Pizza Parma','Primanti Bros','Siam Thai Restaurant','The Simple Greek','SlyFox Taphouse','SoFresh','Villa Reale Pizzeria & Restaurant','The Warren','The Yard'], 
        'Cuisine':['American', 'American', 'American','Asian', 'Mexican','Mexican','American','Mediterranean','American','American','American','Italian','Italian','American','Italian','Mexican','Asian','American','Mexican','Asian','American','American','Italian','American','Asian','Mediterranean','American','American','Italian','American','American'],
        'Address':['946 Penn Avenue 412-765-3270', '930 Penn Avenue 412-586-4287', '301 Fifth Avenue 412-338-9100', '522 Third Avenue 412-261-1291', '211 Forbes Avenue 412-224-5586',',971 Liberty Avenue 412-281-9111','Marriott City Center 412-471-4000','130 6th Street 412-261-6442','Three PPH PLace 412-227-0206','310 Forbes Avenue 412-281-1999','501 Grant Street 412-430-0318','111 Market Street 412-281-6100','123 6th Street 412-281-7060','292 Forbes Avenue 412-434-0100','955 Liberty Avenue 412-738-0603','21 Market Square 412-251-0031','305 Wood Street 412-261-6151','301 Fifth Avenue 412-201-6992','210 Forbes Avenue 412-224-4422','903 Penn Avenue 412-471-8424','476 McMasters Way 412-562-2191','20 Market Square 412-566-7925','963 Liberty Avenue 412-577-7300','2 Market Square 412-261-1599','410 First Avenue 412-281-1122','4313 Market Street 412-261-4976','300 Liberty Avenue 412-586-7474','Five PPG Place Suite 100 412-586-7240','628 Smithfield Street 412-391-3963','245 7th Street 412-201-5888','100 Fifth Avenue 412-291-8182'], 
        'Operation':['Local', 'Local', 'Franchise', 'Local', 'Franchise','Franchise','Franchise','Local','Franchise','Local','Franchise','Local','Local','Franchise','Franchise','Local','Local','Franchise','Franchise','Local','Franchise','Franchise','Franchise','Franchise','Local','Local','Franchise','Local','Local','Local','Franchise']} 

# Convert the dictionary into DataFrame  
df = pd.DataFrame(data)

Upvotes: 0

Views: 96

Answers (1)

Allen Qin
Allen Qin

Reputation: 19947

You can first filter by Cusisine and then use sample to pick a random row:

df.loc[df.Cuisine=='Mexican'].sample(1)

    Restaurant              Cuisine Address Operation
18  Moes Southwest Grill    Mexican 210 Forbes Avenue 412-224-4422  Franchise

Upvotes: 1

Related Questions