43885

Python pandas, build a dataframe from 2 dataframes with these properties [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/21786490/pandas-left-outer-join-multiple-dataframes-on-multiple-columns" dir="ltr" rel="nofollow">Pandas left outer join multiple dataframes on multiple columns</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

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

Answer1:

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

Recommend

  • Encryption and decryption between PHP and Java
  • How to change Google Maps bus stops default icon?
  • Can I launch an intent using only javascript in an Android browser?
  • How to use Gson to parse a list of json objects with nested arrays
  • how to read MQTT mosquitto server persisted DB file
  • MySQL subtract from isolated subquery
  • How do I push many Views from one View using UINavigationController
  • Reducing global variables and maintaining function flow
  • Cumulative count of blocks of 1 with 0 separators in a binary vector in R
  • Partial/Full-match value in one RDD to values in another RDD
  • How can I transform an array of characters with a few lines of code to a data.frame?
  • SceneKit: Is it possible to cast an shadow on an Transparent Object?
  • update record in database using jdatabase
  • How to add new index numbers to the upsampled data while preserving the orginal indices one
  • Transpose table then set and rename index
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • How to set elevation color?
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • Does Mobilefirst provide a provision to access web services directly?
  • xtable package: Skipping some rows in the output
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • QLPreviewController hide print button in ios6
  • How to rebase a series of branches?
  • Illegal mix of collations for operation for date/time comparison
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Read text file and split every line in MSBuild
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Release, debug version and Authorization Google?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Hits per day in Google Big Query
  • embed rChart in Markdown
  • Does armcc optimizes non-volatile variables with -O0?