title: Monte Carlo Simulation for Multiple Entries in Python
body: <p>I've been working to create a Monte Carlo simulation that will run through each ID of my dataframe and yield their respective Means and Standard Deviations. I've been able to write the code to get it for any one ID, but not to iterate through the entire list of IDs in my dataframe. So I could write each line individually, but I need the code to iterate through any mutable list of IDs.</p>
  
  <p>Here, I've tried to create a list of lists in which each set of Monte Carlo observations can be stored (and the mean and std could be taken from). I don't believe that will be the most efficient way of coding this, but it's what I know at this point. Is there anyway to run the Monte Carlo simulation on each of the IDs (without specifically calling each)? I need to be able to add and remove various IDs and corresponding data from the list. </p>
  
  <p>This is a follow up on: <a href="https://stackoverflow.com/questions/52173863/utilizing-monte-carlo-to-predict-revenue-in-python">Utilizing Monte Carlo to Predict Revenue in Python</a></p>
  
  <pre><code>import pandas as pd
  import matplotlib.pyplot as plt
  import numpy as np
  
  
  ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  Revenue = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800, 1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
  odds = [0.5, 0.6, 0.33, 0.1, 0.9, 0.87, 0.37, 0.55, 0.97, 0.09, 0.5, 0.6, 0.33, 0.1, 0.9, 0.87, 0.37, 0.55, 0.97, 0.09]
  
  d = {'ID': ID, 'Revenue': Revenue, 'Odds': odds}
  df = pd.DataFrame(d)
  df['Expected Value'] = df['Revenue']*df['Odds']
  
  print(df)
  
  num_samples = 100
  df['Random Number'] = np.random.rand(len(df))
  
  def monte_carlo_array(df):
      for _ in range(len(df)):
          yield []
  
  mc_arrays = list(monte_carlo_array(df))
  
  # Fill each list with 100 observations (no filtering necessary)
  id_1 = []
  filter_1 = (df['ID'] == 5)
  
  for _ in range(num_samples):
      sample = df['Revenue'] * np.where(np.random.rand(len(df)) &lt; \
                            df['Odds'], 1, 0)
      for l in monte_carlo_array(df):
          for i in l:
          mc_arrays[i].append(sample.sum())
      id_1.append(sample.loc[filter_1].sum())
  
  
  # Plot simulation results.
  n_bins = 10
  plt.hist([id_1], bins=n_bins, label=["ID: 1"])
  plt.legend()
  plt.title("{} simulations of revenue".format(num_samples))
  
  print(mc_arrays)
  
  df['Monte Carlo Mean'] = np.mean(mc_arrays[0])
  print(df['Monte Carlo Mean'])
  </code></pre>