Column appended in a pandas dataframe malfunctions
<p>I have a dataframe named df1</p>

<pre><code> df1.columns
 Out[55]: Index(['TowerLon', 'TowerLat'], dtype='object')
 df1.shape
 Out[56]: (1141, 2)
 df1.head(3)
 Out[57]: 
        TowerLon   TowerLat
      0 -96.709417  32.731611
      1 -96.709500  32.731722
      2 -96.910389  32.899944
</code></pre>

<p>I also have an np.ndarray named labels:</p>

<pre><code>      type(labels)
      Out[62]: numpy.ndarray
      labels
      Out[63]: array([1, 1, 0, ..., 0, 0, 1])
</code></pre>

<p>I appended the np.ndarray labels to the dataframe df1 using the following command:</p>

<pre><code>      df1["labels"] = labels.tolist()
</code></pre>

<p>The operation was seemingly successful:</p>

<pre><code>      df1.shape
      Out[68]: (1141, 3)

     df1.dtypes
     Out[69]: 
     TowerLon    float64
     TowerLat    float64
     labels        int64
     dtype: object

    df1.head(3)
    Out[70]: 
    TowerLon   TowerLat  labels
  0 -96.709417  32.731611       1
  1 -96.709500  32.731722       1
  2 -96.910389  32.899944       0
</code></pre>

<p>However when I try to list the values of the new column I get an error message:</p>

<pre><code>   df1.labels
   Traceback (most recent call last):
   File "&lt;ipython-input-71-6afd8264be10&gt;", line 1, in &lt;module&gt;
   df1.labels
   File "C:\Users\Alexandros_7\Anaconda3\lib\site-         packages\pandas\core\generic.py", line 2668, in __getattr__
   return object.__getattribute__(self, name)

   AttributeError: 'DataFrame' object has no attribute 'labels'
</code></pre>

<p>I get a similar error message when I try to use labels to form a scatter plot:</p>

<pre><code>    plt.scatter(df1.TowerLat, df1.TowerLon, c=df1.labels) 

    Traceback (most recent call last):
    File "&lt;ipython-input-73-b71c79011e38&gt;", line 1, in &lt;module&gt;
    plt.scatter(df1.TowerLat, df1.TowerLon, c=df1.labels)
     File "C:\Users\Alexandros_7\Anaconda3\lib\site -  packages\pandas\core\generic.py", line 2668, in __getattr__
    return object.__getattribute__(self, name)

    AttributeError: 'DataFrame' object has no attribute 'labels'
</code></pre>

<p>Could you help me?  Thank you!</p>

<p>The <code>AttributeError</code> occurs due to the fact that <code>labels</code> is not part of the original <code>DataFrame</code>. You can however access the data by using the following method:</p>

<pre><code>df1['labels']
</code></pre>

<p>This will give you the following output:</p>

<pre><code>0    1
1    1
2    0
Name: labels, dtype: int64
</code></pre>

<p>for your plotting error also replace <code>df1.labels</code> with the method suggested above as follows:</p>

<pre><code>plt.scatter(df1.TowerLat, df1.TowerLon, c=df1['labels'])
</code></pre>

<p>As for the error with the <code>groupby</code> method change it to:</p>

<pre><code>bylevel=df1.groupby(df1["labels"])
</code></pre>

<p>as <code>levels</code> is not part of the <code>DataFrame</code></p>

python