title: Avoid SettingWithCopyWarning in Pandas
body: <p>I have the following code, I'm not sure how to rewrite it in order to avoid the SettingWithCopyWarning or should I just disable the warning?</p>
  <p>The code is working I just want to assign the left attribute of pd.cut to a new column if the number is positive and the right attribute if negative</p>
  <pre><code>import numpy as np
  import pandas as pd
  
  
  bins = np.array([-1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0])
  test_data = [{&quot;ID&quot;: 1, &quot;Value&quot;: -0.5}, {&quot;ID&quot;: 2, &quot;Value&quot;: 1.5}]
  
  df = pd.DataFrame(test_data)
  
  df[&quot;Bin&quot;] = 0.0
  df[&quot;Bin&quot;][df[&quot;Value&quot;] &gt; 0.0] = [d['left'] for d in [{fn: getattr(f, fn) for fn in ['left']} for f in pd.cut(df[&quot;Value&quot;], bins)]]
  df[&quot;Bin&quot;][df[&quot;Value&quot;] &lt; 0.0] = [d['right'] for d in [{fn: getattr(f, fn) for fn in ['right']} for f in pd.cut(df[&quot;Value&quot;], bins)]]
  
  print(df)
  </code></pre>
  <p>Running the code produces</p>
  <pre><code>test.py:11: SettingWithCopyWarning: 
  A value is trying to be set on a copy of a slice from a DataFrame
  
  See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
    df[&quot;Bin&quot;][df[&quot;Value&quot;] &gt; 0.0] = [d['left'] for d in [{fn: getattr(f, fn) for fn in ['left']} for f in pd.cut(df[&quot;Value&quot;], bins)]]
  e.py:12: SettingWithCopyWarning: 
  A value is trying to be set on a copy of a slice from a DataFrame
  
  See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
    df[&quot;Bin&quot;][df[&quot;Value&quot;] &lt; 0.0] = [d['right'] for d in [{fn: getattr(f, fn) for fn in ['right']} for f in pd.cut(df[&quot;Value&quot;], bins)]]
     ID  Value  Bin
  0   1   -0.5 -0.5
  1   2    1.5  1.0
  </code></pre>