既然从前面的问题中您至少对pandas有点熟悉,那么:
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)
一些解释(请注意,我已经修改file2.csv
,以便有更多的共同元素)。
首先,读取数据:
>>> import pandas as pd
>>> df1 = pd.read_csv("file1.csv", sep=r"\s+")
>>> df2 = pd.read_csv("file2.csv", sep=r"\s+")
>>> 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
>>> df2.head()
Mon-id
0 Mon-000101
1 Mon-000121
2 Mon-000131
3 Mon-000141
4 Mon-000151
然后,我们可以重命名 df2 中的轴:
>>> df2.rename_axis({"Mon-id": "NAME"}).head()
NAME
0 Mon-000101
1 Mon-000121
2 Mon-000131
3 Mon-000141
4 Mon-000151
之后,merge
将简单地做正确的事情:
>>> merged = df1.merge(df2.rename_axis({"Mon-id": "NAME"}))
>>> 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
最后,我们可以写出来,告诉它不要添加索引列:
>>> merged.to_csv("output.csv", index=False)
生成一个看起来像的文件
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