Pandas convert columns type from list to np.array
<p>I'm trying to apply a function to a pandas dataframe, such a function required two np.array as input and it fit them using a well defined model.</p>

<p>The point is that I'm not able to apply this function starting from the selected columns since their "rows" contain list read from a JSON file and not np.array.</p>

<p>Now, I've tried different solutions:</p>

<pre><code>#Here is where I discover the problem

train_df['result'] = train_df.apply(my_function(train_df['col1'],train_df['col2']))

#so I've tried to cast the Series before passing them to the function in both these ways:

X_col1_casted = trai_df['col1'].dtype(np.array)
X_col2_casted = trai_df['col2'].dtype(np.array)
</code></pre>

<p>doesn't work.</p>

<pre><code>X_col1_casted = trai_df['col1'].astype(np.array)
X_col2_casted = trai_df['col2'].astype(np.array)
</code></pre>

<p>doesn't work.</p>

<pre><code>X_col1_casted = trai_df['col1'].dtype(np.array)
X_col2_casted = trai_df['col2'].dtype(np.array)
</code></pre>

<p>does'nt work.</p>

<p>What I'm thinking to do now is a long procedure like:</p>

<p>starting from the uncasted column-series, convert them into list(), iterate on them apply the function to the np.array() single elements, and append the results into a temporary list. Once done I will convert this list into a new column. ( clearly, I don't know if it will work )</p>

<p>Does anyone of you know how to help me ?</p>

<p>EDIT:
I add one example to be clear:</p>

<p>The function assume to have as input two np.arrays. Now it has two lists since they are retrieved form a json file. The situation is this one:</p>

<pre><code>col1        col2    result
[1,2,3]     [4,5,6]  [5,7,9]
[0,0,0]     [1,2,3]  [1,2,3]
</code></pre>

<p>Clearly the function is not the sum one, but a own function. For a moment assume that this sum can work only starting from arrays and not form lists, what should I do ?</p>

<p>Thanks in advance </p>

<p>Use <code>apply</code> to convert each element to it's equivalent array:</p>

<pre><code>df['col1'] = df['col1'].apply(lambda x: np.array(x))

type(df['col1'].iloc[0])
numpy.ndarray
</code></pre>

<p>Data:</p>

<pre><code>df = pd.DataFrame({'col1': [[1,2,3],[0,0,0]]})
df
</code></pre>

<p><a href="http://i.stack.imgur.com/F2F0B.png" rel="nofollow"><img src="http://i.stack.imgur.com/F2F0B.png" alt="Image"></a></p>

python pandas numpy dataframe casting