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
Comments
Post a Comment