1

I would like to assign constant numpy array value to pandas dataframe column.

Here is what I tried:

import pandas as pd
import numpy as np

my_df = pd.DataFrame({'col_1': [1,2,3], 'col_2': [4,5,6]})
my_df['new'] = np.array([]) # did not work
my_df['new'] = np.array([])*len(df) # did not work

Here is what worked:

my_df['new'] = my_df['new'].apply(lambda x: np.array([]))

I am curious why it works with simple scalar, but does not work with numpy array. Is there simpler way to assign numpy array value?

6
  • 1
    For list-like values it expects to set as column series, so expects equal lengths. apply method is like forloop setting for each row. Commented Oct 16, 2017 at 15:44
  • You want your data-frame column to contain a bunch of empty numpy.ndarray objects? Commented Oct 16, 2017 at 15:46
  • @juanpa.arrivillaga correct. It is like a placeholder, I will be concatenating to it later. Commented Oct 16, 2017 at 15:49
  • 2
    This would be incredibly inefficient from many perspectives. It doesn't sound like you should be using pandas/numpy at all. Commented Oct 16, 2017 at 15:50
  • 2
    @user1700890 Maybe you should open a seperate question describing what your problem is that you actually trying to solve, what you did so far and give some sample data. Commented Oct 16, 2017 at 16:06

1 Answer 1

2

Your "new" column will contains arrays, so it must be a object type column.

The simplest way to initialize it is :

my_df = pd.DataFrame({'col_1': [1,2,3], 'col_2': [4,5,6]})
my_df['new']=None

You can then fill it as you want. For example :

for index,(a,b,_)  in my_df.iterrows():
    my_df.loc[index,'new']=np.arange(a,b)
#     
#    col_1  col_2        new
# 0      1      4  [1, 2, 3]
# 1      2      5  [2, 3, 4]
# 2      3      6  [3, 4, 5]    
Sign up to request clarification or add additional context in comments.

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.