title: Finding counts of relative and absolute fluctuations in dataframe where each row contains a timeseries
body: <p>I have a dataframe containing a table of financial timeseries, with each row having the columns:</p>
  
  <ul>
  <li><code>ID</code> of that timeseries</li>
  <li>a <code>Target</code> value (against which we want to measure deviations, both relative and absolute)</li>
  <li>and a timeseries of values for various dates: <code>1/01, 1/02, 1/03, ...</code></li>
  </ul>
  
  <p><strong>We want to calculate the fluctuation counts, both relative and absolute, for every row/ID's timeseries. Then we want to find which row/ID has the most fluctuations/'spikes', as follows</strong>:</p>
  
  <ul>
  <li>First, we find difference between two timeseries values and estimate a <em>threshold</em>. Threshold represents how much difference is allowed between two values before we declare that a 'fluctuation' or 'spike'. If the difference is higher than the threshold you set, between any two columns's values then it's a spike.
  
  <ul>
  <li>However, <strong>we need to ensure that the threshold is generic and works with both % and absolute values between any two values in any row</strong>.</li>
  <li>So basically, we find a threshold in a percentage form (make an educated prediction) as we have one row values represented in "%" form. Plus, '%' form will also work properly with the absolute value as well.</li>
  </ul></li>
  <li>The output should be a new column fluctuation counts (<code>FCount</code>), both relative and absolute, for every row/ID.</li>
  </ul>
  
  <p>Code:</p>
  
  <pre><code>import pandas as pd 
  
  # Create sample dataframe
  raw_data = {'ID': ['A1', 'B1', 'C1', 'D1'], 
    'Domain': ['Finance', 'IT', 'IT', 'Finance'], 
    'Target': [1, 2, 3, 0.9%], 
    'Criteria':['&lt;=', '&lt;=', '&gt;=', '&gt;='],
    "1/01":[0.9, 1.1, 2.1, 1],
    "1/02":[0.4, 0.3, 0.5, 0.9], 
    "1/03":[1, 1, 4, 1.1], 
    "1/04":[0.7, 0.7, 0.1, 0.7],
    "1/05":[0.7, 0.7, 0.1, 1], 
    "1/06":[0.9, 1.1, 2.1, 0.6],}
  
  df = pd.DataFrame(raw_data, columns = ['ID', 'Domain', 'Target','Criteria', '1/01', 
    '1/02','1/03', '1/04','1/05', '1/06'])
  
     ID   Domain  Target Criteria  1/01  1/02  1/03  1/04  1/05  1/06  
  0  A1  Finance       1       &lt;=   0.9   0.4   1.0   0.7   0.7   0.9  
  1  B1       IT       2       &lt;=   1.1   0.3   1.0   0.7   0.7   1.1  
  2  C1       IT       3       &gt;=   2.1   0.5   4.0   0.1   0.1   2.1  
  3  D1  Finance     0.9%      &gt;=   1.0   0.9   1.1   0.7   1.0   0.6
  </code></pre>
  
  <p>And here's the expect output with a fluctuation count (<code>FCount</code>) column. Then we can get whichever ID has the largest FCount.</p>
  
  <pre><code>   ID   Domain  Target Criteria  1/01  1/02  1/03  1/04  1/05  1/06  FCount
  0  A1  Finance       1       &lt;=   0.9   0.4   1.0   0.7   0.7   0.9    -
  1  B1       IT       2       &lt;=   1.1   0.3   1.0   0.7   0.7   1.1    - 
  2  C1       IT       3       &gt;=   2.1   0.5   4.0   0.1   0.1   2.1    - 
  3  D1  Finance     0.9%      &gt;=   1.0   0.9   1.1   0.7   1.0   0.6    -
  </code></pre>