Pandas DataFrame.update with MultiIndex label -


given dataframe multiindex , dataframe b one-dimensional index, how update column values of new values b index of b should matched second index label of a.

test data:

begin = [10, 10, 12, 12, 14, 14] end = [10, 11, 12, 13, 14, 15] values = [1, 2, 3, 4, 5, 6] values_updated = [10, 20, 3, 4, 50, 60]  multiindexed = pd.dataframe({'begin': begin,                              'end': end,                              'value': values}) multiindexed.set_index(['begin', 'end'], inplace=true)  singleindexed = pd.dataframe.from_dict(dict(zip([10, 11, 14, 15],                                             [10, 20, 50, 60])),                                    orient='index') singleindexed.columns = ['value'] 

and desired result should be

               value begin end        10    10       10       11       20 12    12       3       13       4 14    14       50       15       60 

now thinking variant of

multiindexed.update(singleindexed) 

i searched docs of dataframe.update, not find w.r.t. index handling.

am missing easier way accomplish this?

you can use loc selecting data in multiindexed , set new values values:

print singleindexed.index int64index([10, 11, 14, 15], dtype='int64')  print singleindexed.values [[10]  [20]  [50]  [60]]  idx = pd.indexslice  print multiindexed.loc[idx[:, singleindexed.index],:]            value start end        10    10       1       11       2 14    14       5       15       6  multiindexed.loc[idx[:, singleindexed.index],:] = singleindexed.values print multiindexed            value start end        10    10      10       11      20 12    12       3       13       4 14    14      50       15      60 

using slicers in docs.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -