Skip to content

BUG/ENH: multi-index assignment #4116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jreback opened this issue Jul 3, 2013 · 0 comments · Fixed by #6301
Closed

BUG/ENH: multi-index assignment #4116

jreback opened this issue Jul 3, 2013 · 0 comments · Fixed by #6301
Assignees
Labels
Bug Enhancement Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Jul 3, 2013

http://stackoverflow.com/questions/17451843/copying-a-single-index-dataframe-into-a-multiindex-dataframe/17452237#17452237

IIn [3]: import itertools

In [4]: inner = ('a','b')

In [5]: outer = ((10,20), (1,2))

In [6]: cols = ('one','two','three','four')

In [7]: sngl = pd.DataFrame(np.random.randn(2,4), index=inner, columns=cols)

IIn [8]: index_tups = list(itertools.product(*(outer + (inner,))))

In [9]: index_mult = pd.MultiIndex.from_tuples(index_tups)

In [10]: mult = pd.DataFrame(index=index_mult, columns=cols)

and the solution

    In [48]: nm = mult.reset_index().set_index('level_2')

    In [49]: nm
    Out[49]: 
             level_0  level_1  one  two three four
    level_2                                       
    a             10        1  NaN  NaN   NaN  NaN
    b             10        1  NaN  NaN   NaN  NaN
    a             10        2  NaN  NaN   NaN  NaN
    b             10        2  NaN  NaN   NaN  NaN
    a             20        1  NaN  NaN   NaN  NaN
    b             20        1  NaN  NaN   NaN  NaN
    a             20        2  NaN  NaN   NaN  NaN
    b             20        2  NaN  NaN   NaN  NaN

A bug here????
This should probably work with a series on the rhs; this might be a buglet

    In [50]: nm.loc['a',sngl.columns] = sngl.loc['a'].values

    In [51]: nm
    Out[51]: 
             level_0  level_1        one        two     three        four
    level_2                                                              
    a             10        1  0.3738456 -0.2261926 -1.205177  0.08448757
    b             10        1        NaN        NaN       NaN         NaN
    a             10        2  0.3738456 -0.2261926 -1.205177  0.08448757
    b             10        2        NaN        NaN       NaN         NaN
    a             20        1  0.3738456 -0.2261926 -1.205177  0.08448757
    b             20        1        NaN        NaN       NaN         NaN
    a             20        2  0.3738456 -0.2261926 -1.205177  0.08448757
    b             20        2        NaN        NaN       NaN         NaN

    In [52]: nm.reset_index().set_index(['level_0','level_1','level_2'])
    Out[52]: 
                                   one        two     three        four
    level_0 level_1 level_2                                            
    10      1       a        0.3738456 -0.2261926 -1.205177  0.08448757
                    b              NaN        NaN       NaN         NaN
            2       a        0.3738456 -0.2261926 -1.205177  0.08448757
                    b              NaN        NaN       NaN         NaN
    20      1       a        0.3738456 -0.2261926 -1.205177  0.08448757
                    b              NaN        NaN       NaN         NaN
            2       a        0.3738456 -0.2261926 -1.205177  0.08448757
                    b              NaN        NaN       NaN         NaN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Enhancement Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant