Pandas Merge - put all join-column data under one output column instead of two?
<p>I have two CSV files, with the following schemas:</p>

<p>CSV1 columns:</p>

<pre><code>"Id","First","Last","Email","Company"
</code></pre>

<p>CSV2 columns:</p>

<pre><code>"PersonId","FirstName","LastName","Em","FavoriteFood"
</code></pre>

<p>If I load them each into a Pandas DataFrame and do <code>newdf = df1.merge(df2, how='outer', left_on=['Last', 'First'], right_on=['LastName','FirstName'])</code></p>

<p>Then a CSV export of the joined DataFrame has a schema of:</p>

<pre><code>"Id","First","Last","Email","Company","PersonId","FirstName","LastName","Em","FavoriteFood"
</code></pre>

<ul>
<li>All the rows that were only in CSV1 have a first name printed under
"First."</li>
<li>All the rows that were only in CSV2 have a first name printed under
"FirstName."</li>
<li>All the rows that were in both CSV file have a first
name <em>(the exact same value - which is to be expected, since it was a
"join on" value)</em> printed under both columns.</li>
<li>Same problem for "Last" &amp; "LastName."</li>
</ul>

<p>What I'd like is an output schema more like this:</p>

<pre><code>"Id","First","Last","Email","Company","PersonId","Em","FavoriteFood"
</code></pre>

<ul>
<li>It should have all of the "first names" under the column "First" (and equivalent for "Last").</li>
</ul>

<p>Most relational database software I'm familiar with does this <em>(the left-side join-column names win the naming war)</em>.  Does Pandas have a syntax for instructing it to do so?</p>

<p>I can do <code>df1.merge(df2.rename(columns = {'LastName':'Last', 'FirstName':'First'}), how='outer', on=['Last', 'First'])</code>, but stylistically, it drives me crazy to hard-code the same column-names twice in my source code.  It's more to fix if I change the column names in the CSV files.</p>

<p>One way to do it would be to just merge the same way but drop columns that you'd like to remove.</p>

<pre><code>newdf.drop(['LastName','FirstName'], 1, inplace=True)
</code></pre>

python join merge rename