パンダの使用:
import pandas as pd
df1 = pd.read_csv("coord1.csv")
df2 = pd.read_csv("coord2.csv")
combined = df1.merge(df2, how='outer').fillna(0)
combined.sort(list(combined.columns[:2]), inplace=True)
combined.to_csv("coord_merged.csv",index=False)
まず、元のデータを読み込みます。
>>> import pandas as pd
>>> df1 = pd.read_csv("coord1.csv")
>>> df2 = pd.read_csv("coord2.csv")
>>> df1
x-coordinate y-coordinate data 1 data 2
0 1 10 20 0
1 5 15 1 2
>>> df2
x-coordinate y-coordinate data 3 data 4
0 1 10 7 8
1 3 25 1 2
それらをマージし、欠落しているデータをゼロで埋めます:
>>> combined = df1.merge(df2, how='outer')
>>> combined
x-coordinate y-coordinate data 1 data 2 data 3 data 4
0 1 10 20 0 7 8
1 5 15 1 2 NaN NaN
2 3 25 NaN NaN 1 2
>>> combined = df1.merge(df2, how='outer').fillna(0)
>>> combined
x-coordinate y-coordinate data 1 data 2 data 3 data 4
0 1 10 20 0 7 8
1 5 15 1 2 0 0
2 3 25 0 0 1 2
選別:
>>> combined.sort(list(combined.columns[:2]), inplace=True)
>>> combined
x-coordinate y-coordinate data 1 data 2 data 3 data 4
0 1 10 20 0 7 8
2 3 25 0 0 1 2
1 5 15 1 2 0 0
そして最後に次のように書きます。
>>> combined.to_csv("coord_merged.csv",index=False)
>>> !cat coord_merged.csv
x-coordinate, y-coordinate, data 1, data 2, data 3, data 4
1.0,10.0,20.0,0.0,7.0,8.0
3.0,25.0,0.0,0.0,1.0,2.0
5.0,15.0,1.0,2.0,0.0,0.0
整数形式を維持することが重要な場合は、
>>> combined.astype(int).to_csv("coord_merged.csv",index=False)
>>> !cat coord_merged.csv
x-coordinate, y-coordinate, data 1, data 2, data 3, data 4
1,10,20,0,7,8
3,25,0,0,1,2
5,15,1,2,0,0
それをするでしょう。