17

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

4 Answers 4

26

This is the simplest way:

df['column_name']=pd.Series(arr)
Sign up to request clarification or add additional context in comments.

Comments

8

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]) })

Comments

1

That will work

import pandas as pd
import numpy as np

df = pd.DataFrame()

for i in range(5):
    arr = np.random.rand(10)
    df[i] = arr

Maybe a simpler way is to use the vectorization

arr = np.random.rand(10, 5)
df = pd.DataFrame(arr)

Comments

1

Another way if you have a 2d array:

pd.DataFrame(np.matrix.transpose(array))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.