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!

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

Reset Code Python Output:
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.

Reset Code Python Output:
524

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

Reset Code Python Output:
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

Reset Code Python Output:

      652
      no
    
    
      653
      no
    
    
      654
      no
    
    
      655
      no
    
    
      656
      no
    
    
      657
      no
    
    
      658
      no
    
    
      659
      no
    
    
      660
      no
    
    
      661
      no
    
    
      662
      no
    
    
      663
      no
    
    
      664
      no
    
    
      665
      no
    
  

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

Reset Code Python Output:
76

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: