Append a column to a dataframe in Pandas
<p>I am trying to append a numpy.darray to a dataframe with little success.
The dataframe is called user2 and the numpy.darray is called CallTime.</p>

<p>I tried:</p>

<pre><code>user2["CallTime"] = CallTime.values
</code></pre>

<p>but I get an error message:</p>

<pre><code>Traceback (most recent call last):
File "&lt;ipython-input-53-fa327550a3e0&gt;", line 1, in &lt;module&gt;
user2["CallTime"] = CallTime.values
AttributeError: 'numpy.ndarray' object has no attribute 'values'
</code></pre>

<p>Then I tried:</p>

<pre><code>user2["CallTime"] = user2.assign(CallTime = CallTime.values)
</code></pre>

<p>but I get again the same error message as above.</p>

<p>I also tried to use the merge command but for some reason it was not recognized by Python although I have imported pandas.  In the example below CallTime is a dataframe:</p>

<pre><code> user3 = merge(user2, CallTime)
</code></pre>

<p>Error message:</p>

<pre><code>  Traceback (most recent call last):
  File "&lt;ipython-input-56-0ebf65759df3&gt;", line 1, in &lt;module&gt;
  user3 = merge(user2, CallTime)
  NameError: name 'merge' is not defined
</code></pre>

<p>Any ideas?</p>

<p>Thank you!</p>

<p>I think instead to provide only documentation, I will try to provide a sample:</p>

<pre><code>import numpy as np
import pandas as pd

data = {'A': [2010, 2011, 2012],
    'B': ['Bears', 'Bears', 'Bears'],
    'C': [11, 8, 10],
    'D': [5, 8, 6]}
user2 = pd.DataFrame(data, columns=['A', 'B', 'C', 'D'])

#creating the array what will append to pandas dataframe user2
CallTime = np.array([1, 2, 3])

#convert to list the ndarray array CallTime, if you your CallTime is a matrix than after converting to list you can iterate or you can convert into dataframe and just append column required or just join the dataframe.

user2.loc[:,'CallTime'] = CallTime.tolist()


print(user2)
</code></pre>

<p><a href="http://i.stack.imgur.com/GuchR.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/GuchR.jpg" alt="result of dataframe user2"></a></p>

<p>I think this one will help, also check <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html" rel="nofollow">numpy.ndarray.tolist</a> documentation if need to find out why we need the list and how to do, also here is example how to create dataframe from numpy in case of need <a href="http://stackoverflow.com/a/35245297/2027457">http://stackoverflow.com/a/35245297/2027457</a></p>

python numpy