python - Duplicating Values based on Series input -
i'm trying write custom function extends replace functionality (with domain specific concerns) have series looks like:
0 1 1 2 2 4 3 4 4 4
and return values function (which can't modify unfortunately cause it's coming source) looks either series like:
1 2 b 4 c
or dataframe looks this
cola colb colc 1 t f 2 b f f 4 c t t
i'm trying return output either looks like
0 1 b 2 c 3 c 4 c
or
cola colb colc 0 t f 1 b f f 2 c t t 3 c t t 4 c t t
depending on type returned function. i'm able write script performs iteratively, feel there must more efficient, more pandas specific way of performing operation, before generate hideous nested monstrosity figured i'd check if there's well-supported way!
i don't know how data looks may need modify code following works using map
:
in [32]: s.map(s1[1]) out[32]: 0 0 1 b 2 c 3 c 4 c name: 1, dtype: object
for second 1 can perform left merge
have construct dataframe series:
in [41]: pd.dataframe(s).merge(df, left_on=[1], right_index=true, how='outer') out[41]: 1 cola colb colc 0 0 1 t f 1 2 b f f 2 4 c t t 3 4 c t t 4 4 c t t
for above can set index after merge
edit
actually second problem it's easier use reindex
, pass series values:
in [42]: df.reindex(s) out[42]: cola colb colc 1 1 t f 2 b f f 4 c t t 4 c t t 4 c t t
Comments
Post a Comment