Conditional Data Simulation Examples in Python


When we do conditional data simulation in Python, we need to combine conditionals and for loops and use them to make simulations in Python!

Before we begin, let's import our pandas library and import random:

import pandas as pd
import random
Importing Pandas Library and random

Example 1: Choosing A Restaurant for Dinner

You and your friend wants to decide which restaurant to go for dinner, but you want to get BBQ Chicken and your friend wants to go to McDonalds. You decide to write code to make a decision!

You use Python to simulate tossing a coin 1,000 times where "head" means BBQ Chicken and "tails" means McDonalds. If you have more heads than tails, you'll go to BBQ Chicken. If you have more tails than heads, you'll go to McDonalds.

Solution Strategy

Since you want to toss a coin 1,000 times (and that would take a long time in real life...), it makes you think of simulations in python which can help you model the real-world process of tossing a coin.

In addition, there are conditions you need to fulfill which help you to choose: head means "BBQ Chicken" and tails means "McDonald". It makes you think about the application of an if-statement in python. If there is a head, it means choose "BBQ Chicken". If there is a tail, it means choose "McDonalds".

Now you can try to create this process through python code!

Python Code

data = []
for i in range(1000):
  coin = random.choice(["heads", "tails"])
  if coin == "heads":
    d = {"which restaurant": "BBQ Chicken"}
  else:
    d = {"which restaurant": "McDonalds"}
  data.append(d)
df = pd.DataFrame(data)
df.tail(20)
which restaurant
980McDonalds
981BBQ Chicken
982BBQ Chicken
983BBQ Chicken
984McDonalds
985BBQ Chicken
986BBQ Chicken
987McDonalds
988McDonalds
989BBQ Chicken
990McDonalds
991McDonalds
992McDonalds
993BBQ Chicken
994BBQ Chicken
995McDonalds
996McDonalds
997McDonalds
998McDonalds
999BBQ Chicken
Simulation of flipping a coin for choosing a restaurant for dinner

We can check our result by find the length of "BBQ Chicken" or "McDonalds" in the table.

len( df[df["which restaurant"] == "BBQ Chicken"] )

524

Restaurant Analysis, showing the number of results where BBQ was chosen

Our results show that we got 524 "heads", or in this case, votes for BBQ Chicken and in turn, 476 "tails", or votes for McDonalds. Since 524 is larger than 476, so based on the results, you and your friend will eat BBQ Chicken!

Example 2: OOTD decision

Suppose you have green, white, and black tops and bottoms. You don’t want to waste time to choose which color to wear everyday. So you want to write a Python code to help you choose each of your Outfit of the Day (OOTD) for one week. Your only requirement is that you do not want to wear the same color top and bottom for your outfit.

Solution Strategy

Since you want to choose your outfit for 1 week, it makes you think of simulations in Python which help you to model a real-world process 7 times.

There are conditions you need to fulfill to help you to choose: don't wear the same color for the top and bottom. It makes you think about the application of an if-statement in Python. If you wear a green top, it means you can only choose white or black bottoms. If you wear a white top, it means you can only choose green or black bottoms. If you wear a black top, it means you can only choose green or white bottoms.

Now you can try to fulfill this process through Python code!

Python Code

data = []
for i in range(7):
  topcolor = random.choice(["green", "white", "black"])
  if topcolor == "green":
    bottomcolor = random.choice(["white", "black"])
  if topcolor == "white":
    bottomcolor = random.choice(["green", "black"])
  if topcolor == "black":
    bottomcolor = random.choice(["white", "green"])
  d = {"topcolor": topcolor, "bottomcolor" : bottomcolor}
  data.append(d)
df = pd.DataFrame(data)
df
topcolorbottomcolor
0greenblack
1blackwhite
2greenblack
3greenblack
4blackwhite
5blackgreen
6greenwhite
Simulation of choosing an outfit of the day for an entire week

Nice! Now you have your OOTD choices for a week!

Example 3: Lucky Number

6 is Data’s lucky number. Suppose Xin wants to roll a 10-sided die 666 times and check how many times she gets her lucky number.

Solution Strategy

Since she wants to roll a die 666 times in order to find how many times her lucky number appears, she can use a simulation in Python!

There is a condition that if she rolls his lucky number, she should put it in your DataFrame. So, she can put a "yes" in his DataFrame every time she roll a 6 and put a "no" in her DataFrame every time she rolls a number which is not a 6.

Now Xin can try to fulfill this process through Python code!

Python Code


data = [ ]
for i in range(666):
  lucky_number = random.randint(1,10)
  if lucky_number == 6:
    d = {"lucky_number": "yes" }
  else:
    d = {"lucky_number": "no" }
  data.append(d)
df = pd.DataFrame(data)
df
652no653no654no655no656no657no658no659no660no661no662no663no664no665no
Simulation of rolling a ten-sided die for the lucky number 6

We can check Xin's results by find the length of "yes" in the table:

len(df[df.lucky_number == "yes"])

76

Number of times Xin rolled in six in the simulation

So we know that Xin got her lucky number "6" 76 times when rolling a 10-sided dice 666 times.

Lecture Topics

Detailed explanations of conditionals and data simulations are a part of the DISCOVERY course content: