Adding attribute from the different table with Pandas
<p>I am using Pandas to process table.</p>

<pre><code>[table1]
  sample1 sample2 sample3
A 11      22      33
B 1       2       3

[table2]
  sample3 sample4 sample2
D 333     444     222

[Result]
  sample1 sample2 sample3
A 11      22      33
B 1       2       3
D NaN     222     333
</code></pre>

<p>I have two tables, and I want to add row <code>D</code> (of table 2) to table 1, considering the column name. If the column in table 1 exists in table 2, the corresponding value of <code>D</code> is added to table 1, like sample 2 and sample 3. If the column in table does not exist in table 2 like sample 1, the value of D is set to <code>NaN</code> or ignored.</p>

<p>Is there any simple way to do this with Pandas?</p>

<p>You can generalize jezrael's <a href="http://stackoverflow.com/a/39404774/3635816">answer</a> by first subselecting columns from <code>table2</code> which are in <code>table1</code>. This is quite neatly done using <a href="http://stackoverflow.com/a/39404774/3635816"><code>numpy.in1d</code></a>. This also avoids forming a potentially huge temporary data frame with columns from both data frames. Example:</p>

<pre><code>import numpy as np
import pandas as pd

table1 = pd.DataFrame([[11, 22, 33], [1, 2, 3]], index=list('AB'), columns=['sample1', 'sample2', 'sample3'])
table2 = pd.DataFrame([[333, 444, 222]], index=['D'], columns=['sample3', 'sample4', 'sample2'])

# Sub-select columns...
cols_in_table1 = table2.columns[np.in1d(table2.columns, table1.columns)]

# ... and concatenate.
results = pd.concat((table1, table2[cols_in_table1]))

print(results)
</code></pre>

<p>Which prints:</p>

<pre><code>   sample1  sample2  sample3
A     11.0       22       33
B      1.0        2        3
D      NaN      222      333
</code></pre>

python pandas dataframe concat