I am trying to map the data between two data frames (the data are from csv file using pandas) but I got the indexes problem
the first df is df1
0 CAT11/06
1 CAT11/100
2 CAT23/223
3 CAT33/14
4 CAT13/14
5 DOG12/04
6 DOG24/14
7 CAT24/112
the second df is df2
0 CAT1
3 CAT2
4 CAT3
5 DOG1
6 DOG2
and I would like to make a new data frame to collect the expected data like this and define it as a df3
0 CAT1 CAT11/06 | CAT11/100 | CAT13/14
1 CAT2 CAT23/223 | CAT24/112
2 CAT3 CAT33/14
3 DOG1 DOG12/04
4 DOG2 DOG24/14
Using two for loop with in and join
df2['col2']=['|'.join(df1[[x in y for y in df1]].tolist()) for x in df2.col1]
df2
Out[347]:
col1 col2
0 CAT1 CAT11/06|CAT11/100|CAT13/14
3 CAT2 CAT23/223|CAT24/112
4 CAT3 CAT33/14
5 DOG1 DOG12/04
6 DOG2 DOG24/14
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With