Question

How can I use a panda row as index for a numpy array? Say I have

>>> grid = arange(10,20)
>>> df = pd.DataFrame([0,1,1,5], columns=['i'])

I would like to do

>>> df['j'] = grid[df['i']]
IndexError: unsupported iterator index

What is a short and clean way to actually perform this operation?

Update

To be precise, I want an additional column that has the values that correspond to the indices that the first column contains: df['j'][0] = grid[df['i'][0]] in column 0 etc

expected output:

index i j 
    0 0 10
    1 1 11
    2 1 11
    3 5 15 

Parallel Case: Numpy-to-Numpy

Just to show where the idea comes from, in standard python / numpy, if you have

>>> keys = [0, 1, 1, 5]
>>> grid = arange(10,20)
>>> grid[keys]
Out[30]: array([10, 11, 11, 15])

Which is exactly what I want to do. Only that my keys are not stored in a vector, they are stored in a column.

Was it helpful?

Solution

This is a numpy bug that surfaced with pandas 0.13.0 / numpy 1.8.0.

You can do:

In [5]: grid[df['i'].values]
Out[5]: array([0, 1, 1, 5])

In [6]: Series(grid)[df['i']]
Out[6]: 
i
0    0
1    1
1    1
5    5
dtype: int64

This matches your output. You can assign an array to a column, as long as the length of the array/list is the same as the frame (otherwise how would you align it?)

In [14]: grid[keys]
Out[14]: array([10, 11, 11, 15])

In [15]: df['j'] = grid[df['i'].values]


In [17]: df
Out[17]: 
   i   j
0  0  10
1  1  11
2  1  11
3  5  15
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top