2017-06-12 78 views
0

如果一个file1.csv和file2.csv包含不同的no。 file1的:在bash或python中合并两个或两个以上的csv?

Header : Node_Name,SI ,type,SI ,type,Node_Name,SI ,type,Node_Name 
Data : Node_0 ,abc,0 ,xyz,0 ,Node_1 ,rwqe,1 ,Node_2 

file2的:

Header : Node_Name,SI,type,Node_Name,SI,type,SI,type,Node_Name,SI,type,SI,type 
Data : Node_0,nbv,0,Node_1,afd,0,mnb,1,Node_2,lm,1,uh,0 

合并文件:如列

Header : Node_Name,SI,TYPE,SI,TYPE,NODE_NAME,SI,TYPE,SI,TYPE,NODE_NAME,SI,TYPE,SI,TYPE 
     Node_0 ,abc,0 ,xyz,0 ,Node_1 ,rwqe,1 ,0 , 0 ,Node_2 ,0 ,0 ,0 ,0 
     Node_0 ,nbv,0 , 0 ,0 ,Node_1 ,afd,0 ,mnb,1 ,Node_2 ,lm,1 ,uh,0 

感谢ADAVNCE

+1

用四个空格前缀代码/数据。请看[编辑帮助](http://stackoverflow.com/editing-help)。 – Cyrus

回答

0

在Python一种可能的解决办法是:

#read all the files 
f1 = pd.read_csv(file1) 
f2 = pd.read_csv(file2) 

#find all unique column names 
col_names = set(list(f1.index) + list(f2.index)) 

#reset column names, any column doesn't exist in current table will be NaN 
f1 = f1.reindex(columns=col_names) 
f2 = f2.reindex(columns=col_names) 

#Now combine those 
combined = pd.concat([f1, f2], aixs=0) 

#deal with missing values 
combined = combined.fillna(0) 

等待一个很好的解决方案庆典:)

+0

它看起来像'找到所有唯一列名'在这里不适用,因为文件具有非唯一列 –

+0

是的它不唯一的:) –

0
while read line; do 
    echo "$(cut -d, -f-8 <<<"$line"),0,0,$(cut -d, -f9 <<<"$line"),0,0,0,0" 
done < file1 
while read line; do 
    echo "$(cut -d, -f-3 <<<"$line"),0,0,$(cut -d, -f4- <<<"$line")" 
done < file2 

不是过于优雅,但仍在工作。