Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
map df1 to df2
#1
Hi,
I have two data frames:

df1:
col1    col2   col3
A        JY      1
C        nY      13
MMS    JY      41
K         SA      94

df2:

df1:
col1    col2   col4
A        JY      0
C        nY      45
CJ       Y       27
MMS    JY      67
K         SA      100
Z         KMM   56
I want get col4 value in df1
Reply
#2
No time to try it right now, but wouldn't df1['col4'] = df2['col4'] work?
Reply
#3
but I only want based on df1. I did as below but it also taking df2 rows.

df1['new_col'] = df.merge(df,how=left,on=['col1','col2'])
It also considering df2 even if col1, col2 row not present in df1.
Reply
#4
I really have no idea what you are trying to do. Perhaps a little more explanation along with an example?
Reply
#5
out_df must only contain df1 rows and add col4 map to df2
out_df:
col1    col2   col3   col4
A        JY      1      0
C        nY      13    45
MMS    JY      41    67
K         SA      94    100
Reply
#6
I think you could try to make a new dataframe with four columns and the number of rows of the dataframe number one. Then, you could make a loop to compare and find the common rows between 1 and 2 using the function loc. I am not sure if the merge function can exclude values that are not present in one of the tables, but you could try to take a look to the manual. Maybe there is a flag to find the common values between two tables.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020