0

I have the issue that I would like to set all values for a given column using a numpy vector. Consider the following

import pandas as pd
from numpy import arange
from scipy import random

index = pd.MultiIndex.from_product([arange(0,3), arange(10,15)], names=['A', 'B'])
df = pd.DataFrame(columns=['test'], index=index)
someValues = random.randint(0, 10, size=5)
df.loc[0,:]['test'] = someValues
print someValues
print df.loc[0,:]

The output is

Out[423]: array([9, 2, 7, 2, 4])
Out[422]: 
   test
B      
10  NaN
11  NaN
12  NaN
13  NaN
14  NaN

This comes - I guess - from me not providing the indices to the values. How can I do this operation, given that "the order" in which the values appear in the numpy array is going to be the order in which they should be set onto the column?

1 Answer 1

1

You can do this, but since the right-hand-side (the assignee) is not labeled, it will just assign the first len(assignee) values

In [1]: import pandas as pd

In [2]: from numpy import arange

In [3]: from scipy import random

In [4]: index = pd.MultiIndex.from_product([arange(0,3), arange(10,15)], names=['A', 'B'])

In [5]: df = pd.DataFrame(columns=['test'], index=index)

In [6]: someValues = random.randint(0, 10, size=5)

In [8]: df.loc[0,'test'] = someValues

In [9]: df
Out[9]: 
     test
A B      
0 10    0
  11    8
  12    2
  13    0
  14    2
1 10  NaN
  11  NaN
  12  NaN
  13  NaN
  14  NaN
2 10  NaN
  11  NaN
  12  NaN
  13  NaN
  14  NaN

[15 rows x 1 columns]

If you specify an index then it will assign the matching values.

In [34]: df.loc[:,'test'] = Series(someValues,index=pd.MultiIndex.from_tuples([(0,10),(0,11),(1,10),(1,11),(1,14)]))

In [35]: df
Out[35]: 
      test
A B       
0 10     5
  11     9
  12   NaN
  13   NaN
  14   NaN
1 10     1
  11     0
  12   NaN
  13   NaN
  14     7
2 10   NaN
  11   NaN
  12   NaN
  13   NaN
  14   NaN
Sign up to request clarification or add additional context in comments.

3 Comments

old comment deleted* Setting via df.loc[0, 'test'] seems to work, but df.loc[0, :] does not. When I print these, they look exactly the same. Is this expected behavior?
I edited the post; but this IS on 0.13.1. The code reproduces exactly what is shown.
they are not exactly the same in the case of a multi-index, see the docs: pandas-docs.github.io/pandas-docs-travis/….

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.