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>I think you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html" rel="nofollow"><code>concat</code></a> and then remove column <code>sample4</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html" rel="nofollow"><code>drop</code></a>:</p>

<pre><code>print (pd.concat([table1, table2]).drop('sample4', axis=1))
   sample1  sample2  sample3
A     11.0       22       33
B      1.0        2        3
D      NaN      222      333
</code></pre>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.intersection.html" rel="nofollow"><code>intersection</code></a> for selecting columns in both <code>DataFrames</code> and  then concat subset of <code>table2</code> by these columns:</p>

<pre><code>print (table2.columns.intersection(table1.columns))
Index(['sample2', 'sample3'], dtype='object')

print (pd.concat([table1,table2[table2.columns.intersection(table1.columns)]]))
   sample1  sample2  sample3
A     11.0       22       33
B      1.0        2        3
D      NaN      222      333
</code></pre>

<p>Then if need remove rows with <code>NaN</code> use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna.html" rel="nofollow"><code>dropna</code></a>:</p>

<pre><code>print (pd.concat([table1, table2]).drop('sample4', axis=1).dropna())
   sample1  sample2  sample3
A     11.0       22       33
B      1.0        2        3
</code></pre>

python pandas dataframe concat