# 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:

## 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 | |
---|---|

980 | McDonalds |

981 | BBQ Chicken |

982 | BBQ Chicken |

983 | BBQ Chicken |

984 | McDonalds |

985 | BBQ Chicken |

986 | BBQ Chicken |

987 | McDonalds |

988 | McDonalds |

989 | BBQ Chicken |

990 | McDonalds |

991 | McDonalds |

992 | McDonalds |

993 | BBQ Chicken |

994 | BBQ Chicken |

995 | McDonalds |

996 | McDonalds |

997 | McDonalds |

998 | McDonalds |

999 | BBQ Chicken |

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

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
```

topcolor | bottomcolor | |
---|---|---|

0 | green | black |

1 | black | white |

2 | green | black |

3 | green | black |

4 | black | white |

5 | black | green |

6 | green | white |

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

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

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: