Issue
I have this df:
Source Target Relationship
1 C0003 C0007 Spouse
2 C0004 C1501 Spouse
3 C0042 C1931 Spouse
which contains IDs of married couples. Now I need to duplicate each row so that Each person of the couple is both a Source and a Target, the final outcome should be this:
Source Target Relationship
1 C0003 C0007 Spouse
4 C0007 C0003 Spouse
2 C0004 C1501 Spouse
5 C1501 C0004 Spouse
3 C0042 C1931 Spouse
6 C1931 C0042 Spouse
Thanks in advance for your help!
Solution
You can create a new data frame with column Source
as Target
and Target
as Source
, and then append it to the original data frame:
df.append(df.rename(columns={'Source': 'Target', 'Target': 'Source'})).sort_index()
# Source Target Relationship
#1 C0003 C0007 Spouse
#1 C0007 C0003 Spouse
#2 C0004 C1501 Spouse
#2 C1501 C0004 Spouse
#3 C0042 C1931 Spouse
#3 C1931 C0042 Spouse
#4 C0052 C0932 Spouse
#4 C0932 C0052 Spouse
#5 C0054 C1784 Spouse
#5 C1784 C0054 Spouse
Answered By - Psidom
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.