Reputation: 13
I have, I guess a simple question but I cannot find the right answer. I have two pandas series (let's say "A" and "B") with ID in there (string). Series A is bigger than series B. What I am looking for is a way to have a resulting dataframe with 2 columns where the matching elements are on the same row and if there is a value in A that don't exist in B, to add a NaN.
A B
10368 10368
12567 NaN
13456 13456
... ...
and so on.
I guess the merge function in pandas can be helpful but I could not manage to make it work
Thanks in advance
Upvotes: 1
Views: 110
Reputation: 18647
You could use pd.concat
and boolean index using isin
:
a = pd.Series(['10368', '12567', '13456'])
b = pd.Series(['10368', '13456'])
pd.concat([a, a[a.isin(b)]], axis=1)
[out]
0 1
0 10368 10368
1 12567 NaN
2 13456 13456
Upvotes: 1
Reputation: 743
You can try this:
df.loc[df['A'].isin(df['B']), 'B'] = df['A']
df.loc[~df['A'].isin(df['B']), 'B'] = np.nan
Before:
A B
0 1 2
1 2 1
2 3 4
3 7 0
After:
A B
0 1 1.0
1 2 2.0
2 3 NaN
3 7 NaN
Upvotes: 2