0

Trying to fill a pandas dataframe with an array (let's say np.zeros(200)).

If we want to fill a pandas dataframe with a value:

for col in df.columns:
    df[col].values[:] = 2

it works fine, but doing the same with:

for col in df.columns:
    df[col].values[:] = np.zeros(200)

will not work.

I don't understand why the error is thrown (could not broadcast input array X into shape Y,) since I thought I was populating each value of the dataframe individually, therefore the shape wouldn't matter in my opinion.

2 Answers 2

1

As you may know, pandas is built on top of numpy. When you do :

df[col].values[:] # This return a series

You get a series, it's like a numpy array.

Now when you do

np.zeros(200)

You get a 1-D array, but the error you get tell you that your pandas series have not the same length as your numpy array.

To do what you want you are better to do like this :

df.iloc[:,:] = 0

In general, try to avoid looping over an array, most of the time there is built-in method that will do the job.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming I have an matrix of 200 rows:

df = pd.DataFrame(
    np.random.randint(1, 20,size=(200,3)),
    columns = ['one','two','three']
)
df.head()

    one two three
0   6   11  12
1   9   10  5
2   16  11  6
3   14  5   10
4   19  5   4

When placing: df [col] .values It does not refer to the column itself, it only obtains a series-to-array transformation, even more when putting [:] it is obtaining a memory copy of the obtained array.

The correct form would be:

for col in df.columns:
    df.loc[:, col] = np.zeros(200)
df.head()

    one two three
0   0.0 0.0 0.0
1   0.0 0.0 0.0
2   0.0 0.0 0.0
3   0.0 0.0 0.0
4   0.0 0.0 0.0

Assuming you want to do this work with other arrays that do not contain only zeros

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.