Search code examples
pythonpandasseries

Panda series from an "inverted" list of lists


There is a list of lists idx_of_vals = [[ 3, 7, 10, 12, 9], [8, 0, 5, 1], [ 6, 4, 11, 2]] (say, 13 randomly permuted integers from 0 to 12).

The desired output is a series s:

>>> s
0     1
1     1
2     2
3     0
4     2
5     1
6     2
7     0
8     1
9     0
10    0
11    2
12    0
Name: my_name, dtype: int64

I.e. the elements of s with indices from the 0-th element ([ 3, 7, 10, 12, 9]) of idx_of_vals have values 0 (i.e. its index in idx_of_vals), with indices from the 1-st element of idx_of_vals have values 1, and so on.

Current solution:

s = pd.Series(np.nan, index=np.arange(13), name='my_name')
for val, idx in dict(enumerate(idx_of_vals)).items():
    s.loc[idx] = val
s = s.astype(int)

Question: Is there a more efficient and pythonic way to reach the desired result avoiding for loop?


Solution

  • Swing through pandas dataframes:

    (pd.DataFrame(idx_of_vals)
       .stack()
       .droplevel(level=1)
       .sort_values()
       .index)
    

    Output:

    Int64Index([1, 1, 2, 0, 2, 1, 2, 0, 1, 0, 0, 2, 0], dtype='int64')