Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting numpy array into dataframe column?

How do I convert a numpy array into a dataframe column. Let's say I have created an empty dataframe, df, and I loop through code to create 5 numpy arrays. Each iteration of my for loop, I want to convert the numpy array I have created in that iteration into a column in my dataframe. Just to clarify, I do not want to create a new dataframe every iteration of my loop, I only want to add a column to the existing one. The code I have below is sketchy and not syntactically correct, but illustrates my point.

df = pd.dataframe()
for i in range(5):
   arr = create_numpy_arr(blah) # creates a numpy array
   df[i] = # convert arr to df column
like image 355
Jane Sully Avatar asked Jun 08 '17 00:06

Jane Sully


2 Answers

This is the simplest way:

df['column_name']=pd.Series(arr)
like image 190
user1596433 Avatar answered Oct 07 '22 10:10

user1596433


Since you want to create a column and not an entire DataFrame from your array, you could do

import pandas as pd
import numpy as np

column_series = pd.Series(np.array([0, 1, 2, 3]))

To assign that column to an existing DataFrame:

df = df.assign(column_name=column_series)

The above will add a column named column_name into df.

If, instead, you don't have any DataFrame to assign those values to, you can pass a dict to the constructor to create a named column from your numpy array:

df = pd.DataFrame({ 'column_name': np.array([0, 1, 2, 3]) })
like image 33
Julio Cezar Silva Avatar answered Oct 07 '22 09:10

Julio Cezar Silva