title: Pandas: Include rows where column_x.min() and column_y between two values
body: <p>I have a dataset containing rows of measurements (weight) in the following format:</p>
  <p><code>user_id</code>, <code>day_n</code>, <code>weight</code></p>
  <ul>
  <li><code>user_id</code> is the identifier of the user. There are multiple rows
  with the same <code>user_id</code>.</li>
  <li><code>day_n</code> is the day number on which the measurement is done.</li>
  <li><code>weight</code> is the weight in kg.</li>
  </ul>
  <p>For removing outliers or incorrect data, I use a min and max value for both the <code>weight</code> and the <code>day_n</code> column.</p>
  <p>For now, I plot all data into a <a href="https://i.stack.imgur.com/w3426.png" rel="nofollow noreferrer">scatter plot</a>.</p>
  <p><strong>My question:</strong></p>
  <p>How can I only include users, which have their first measurement (<code>weight</code>) between two values (<code>min_weight</code> and <code>max_weight</code>)?</p>
  <p>Considering the following example data and <code>min_weight</code> = 70 and <code>max_weight</code> = 75, <code>user_id</code> 1 should be included, but <code>user_id</code> 2 should not.</p>
  <pre><code>user_id, day_n, weight
  1, 0, 72
  1, 28, 70
  1, 68, 69
  2, 5, 76
  2, 28, 80
  2, 78, 78 
  </code></pre>
  <p><strong>I tried:</strong></p>
  <p>I tried to group by <code>user_id</code> and looked for the min() of <code>day_n</code>. I couldn't figure out how to check the <code>weight</code> in this row.</p>
  <p><strong>My code:</strong></p>
  <pre><code>import pandas as pd
  import numpy as np
  import matplotlib.pyplot as plt
  
  df = pd.read_csv('../datasets/measurements_2019-12-31_2021-07-01.csv')
  
  # Min and max weights
  df = df[df['weight'] &gt; 25]
  df = df[df['weight'] &lt; 300]
  
  # Min and max days
  df = df[df['day_n'] &gt; -7]
  df = df[df['day_n'] &lt; 187]
  
  # Only include rows with &gt; 3 measurements
  df = df.groupby('user_id').filter(lambda x: len(x) &gt; 3)
  
  # Scatter plot
  plt.scatter(df['day_n'], df['weight'])
  
  # Regression line
  m, b = np.polyfit(df['day_n'], df['weight'], 1)
  plt.plot(df['day_n'], m*df['day_n']+b,color='red')
  
  plt.show()
  </code></pre>