0

i'm in need to solve this issue.

I need to build a whole dataframe from two dataframes, but include only certain info from a second Dataframe if required.

EXAMPLE:

DF1:

    MATERIAL_N° Description DATE DUE
0   123123300   Lightbulb X 01/05/2018
1   220466      Lightbulb Y 04/04/2018
2   220000      Lightbulb Z 07/07/2018
3   1241241     Lightbulb A 02/01/2019
4   7775447     Lightbulb B 02/01/2019

DF2:

    BG GROUP    MATERIAL N° TRANSIT TIME
0   9001        123123300   45D
1   9002        220466      30D
2   9004        220000      30D
3   9003        44124       20D
4   9000        2512222     15D
5   9002        1241241     40D

EXPECTED RESULT DF3:

    MATERIAL N° Description     DATE DUE    BG GROUP TRANSIT TIME
0   123123300   Lightbulb X     01/05/2018  9001.0  45D
1   220466      Lightbulb Y     04/04/2018  9002.0  30D
2   220000      Lightbulb Z     07/07/2018  9004.0  30D
3   1241241     Lightbulb A     02/01/2019  9002.0  40D
4   7775447     Lightbulb B     02/01/2019  NaN     NaN

I hope this example is clear enough, i need to make a big dataframe (DF3) adding info in adjoint columns, only in those cases where there is info avaliable from DF2, otherwise leave empty that position.

THANKS! JL

Vaishali
  • 37,545
  • 5
  • 58
  • 86
Javilg
  • 11
  • 1
  • did you try `merge ` ? https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html – BENY Sep 22 '17 at 15:48

1 Answers1

0

You need merge with parameter left

df1.merge(df2, how = 'left')

    MATERIAL N° Description DATE DUE    BG GROUP    TRANSIT TIME
0   123123300   Lightbulb X 01/05/2018  9001.0      45D
1   220466      Lightbulb Y 04/04/2018  9002.0      30D
2   220000      Lightbulb Z 07/07/2018  9004.0      30D
3   1241241     Lightbulb A 02/01/2019  9002.0      40D
4   7775447     Lightbulb B 02/01/2019  NaN         NaN
Vaishali
  • 37,545
  • 5
  • 58
  • 86
  • i used merge and concat method before, this is basically what i asked for, but i missed to ask one point that was if the DF2 had more than those 2 columns and i needed to add columns in a certain order. EX: DF2.columns is (BG GROUP, MATERIAL N°, ETA, ETD, TRANSIT TIME, FLAGS) and i need to get DF3.columns = (MATERIAL N°, DESCRIPTION, DATE DUE, BG_GROUP, TRANSIT_TIME) – Javilg Sep 22 '17 at 15:58
  • If df2 has more columns which you don't need in the final dataframe, you can select the columns of df2 using df1.merge(df2[['BG GROUP', 'MATERIAL N', 'TRANSIT TIME']], how = 'left) – Vaishali Sep 22 '17 at 16:07
  • Thanks Vaishali!! i guess pandas always works, i need to sit back with the docs.... – Javilg Sep 22 '17 at 16:12
  • Wish you all the best:) – Vaishali Sep 22 '17 at 16:19