Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since from earlier questions you're at least a little familiar with <a href="http://pandas.pydata.org" rel="nofollow">pandas</a>, how about:</p> <pre><code>import pandas as pd df1 = pd.read_csv("file1.csv", sep=r"\s+") df2 = pd.read_csv("file2.csv", sep=r"\s+") merged = df1.merge(df2.rename_axis({"Mon-id": "NAME"})) merged.to_csv("merged.csv", index=False) </code></pre> <hr> <p>Some explanation (note that I've modified <code>file2.csv</code> so that there are more elements in common) follows.</p> <p>First, read the data:</p> <pre><code>&gt;&gt;&gt; import pandas as pd &gt;&gt;&gt; df1 = pd.read_csv("file1.csv", sep=r"\s+") &gt;&gt;&gt; df2 = pd.read_csv("file2.csv", sep=r"\s+") &gt;&gt;&gt; df1.head() NAME RA DEC Mean_I1 Mean_I2 0 Mon-000101 100.27242 9.608597 11.082 10.034 1 Mon-000102 100.18012 9.520860 12.296 12.223 2 Mon-000103 100.24811 9.586362 9.429 9.010 3 Mon-000104 100.26741 9.867225 11.811 11.797 4 Mon-000105 100.21005 9.814060 12.087 12.090 &gt;&gt;&gt; df2.head() Mon-id 0 Mon-000101 1 Mon-000121 2 Mon-000131 3 Mon-000141 4 Mon-000151 </code></pre> <p>Then, we can rename the axis in df2:</p> <pre><code>&gt;&gt;&gt; df2.rename_axis({"Mon-id": "NAME"}).head() NAME 0 Mon-000101 1 Mon-000121 2 Mon-000131 3 Mon-000141 4 Mon-000151 </code></pre> <p>and after that, <code>merge</code> will simply do the right thing:</p> <pre><code>&gt;&gt;&gt; merged = df1.merge(df2.rename_axis({"Mon-id": "NAME"})) &gt;&gt;&gt; merged NAME RA DEC Mean_I1 Mean_I2 0 Mon-000101 100.27242 9.608597 11.082 10.034 1 Mon-000121 100.45421 9.685027 11.805 11.777 2 Mon-000131 100.20533 9.397307 -100.000 11.764 3 Mon-000141 100.26134 9.388555 -100.000 12.571 </code></pre> <p>Finally, we can write this out, telling it not to add an index column:</p> <pre><code>&gt;&gt;&gt; merged.to_csv("output.csv", index=False) </code></pre> <p>producing a file which looks like</p> <pre><code>NAME,RA,DEC,Mean_I1,Mean_I2 Mon-000101,100.27242,9.608597,11.082,10.034 Mon-000121,100.45421,9.685027,11.805,11.777 Mon-000131,100.20533,9.397307,-100.0,11.764 Mon-000141,100.26134,9.388555,-100.0,12.571 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload