Reputation: 265
I'm trying to write a custom function that essentially extends replace functionality (with some domain specific concerns) Essentially I have a series that looks like:
0 1
1 2
2 4
3 4
4 4
And return values from another function (Which I can't modify unfortunately cause it's coming from another source) that looks like either a series like:
1 A
2 B
4 C
OR a dataframe that looks like this
cola colb colc
1 A T F
2 B F F
4 C T T
I'm trying to return output that either looks like
0 A
1 B
2 C
3 C
4 C
or
cola colb colc
0 A T F
1 B F F
2 C T T
3 C T T
4 C T T
depending on what type is returned by the function. I'm able to write a script that performs this iteratively, but I feel like there must be a more efficient, more pandas specific way of performing this operation, so before I generate a hideous nested monstrosity I figured I'd check if there's a well-supported way!
Upvotes: 1
Views: 48
Reputation: 393943
I don't know how your data really looks like so you may need to modify my code slightly but the following works using map
:
In [32]:
s.map(s1[1])
Out[32]:
0
0 A
1 B
2 C
3 C
4 C
Name: 1, dtype: object
for the second one you can perform a left merge
but you have to construct a dataframe from your 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 A T F
1 2 B F F
2 4 C T T
3 4 C T T
4 4 C T T
For the above you can set the index after the merge
EDIT
Actually for your second problem it's easier to use reindex
and pass the series values:
In [42]:
df.reindex(s)
Out[42]:
cola colb colc
1
1 A T F
2 B F F
4 C T T
4 C T T
4 C T T
Upvotes: 4
Reputation: 1149
You can use pandas.merge to do that.
s = pd.Series([1,2,4,4,4])
df = pd.DataFrame({'colA':[1,2,4], 'colB':['A','B','C']})
pd.merge(pd.DataFrame(s, columns=['series']), df, left_on='series', right_on='colA')
Output:
series colA colB
0 1 1 A
1 2 2 B
2 4 4 C
3 4 4 C
4 4 4 C
Upvotes: 0