I have the following 2 dataframes
Example1
sku loc flag
122 61 True
123 61 True
113 62 True
122 62 True
123 62 False
122 63 False
301 63 True
Example2
sku dept
113 a
122 b
123 b
301 c
I want to perform a merge, or join opertation using Pandas (or whichever Python operator is best) to produce the below data frame.
Example3
sku loc flag dept
122 61 True b
123 61 True b
113 62 True a
122 62 True b
123 62 False b
122 63 False b
301 63 True c
Both
df_Example1.join(df_Example2,lsuffix='_ProdHier')
df_Example1.join(df_Example2,how='outer',lsuffix='_ProdHier')
Aren't working. What am I doing wrong?
Perform a
left
merge, this will usesku
column as the column to join on:If
sku
is in fact your index then do this:Another method is to use
map
, if you setsku
as the index on your second df, so in effect it becomes a Series then the code simplifies to this:A more generic application would be to use
apply
andlambda
as follows:Add as a new dataframe column
OR replace the existing ('num_letter') column
VLoopup in VBA is just like pandas.dataframe.merge
I always look for so many procedures for VBA in the past and now python dataframe saves me a ton of work, good thing is I don't need write a vlookup method.
pandas.DataFrame.merge
You can also try the following to do a left merge.
outer or left act like SQL, python's built-in class DataFrame has the method merge taking many args, which is very detailed and handy.