2017-04-12 8 views
0

私はpandasを使って、2つのcsvファイルを別の列ヘッダーを持つ可能性のあるものの上にマージしています。私が持っている問題は、ランダムに新しい行に分割されているようです。Python Pandas to_csv余分な行を得る

File 1: 
ID, Height 
0 , 1 
1 , 2 
2 , 3 

File 2: 

ID, Message 
0 , "Long string message" 
1 , "May include tabs, multiple lines \n 
    that go on for a while" 
2 , "More of the same" 

結果は次のようになります。

ID, Height, Message 
0, 1,  '', 
1, 2,  '', 
2, 3,  '', 
0, '', "Long string message", 
1, '', "May include tabs, multiple lines \n 
       that go on for a while", 
2, '', "More of the same" 

私は戻って取得しています何:

ID, Height, Message 
0, 1,  '', 
1, 2,  '', 
2, 3,  '', 
0, '', "Long string message", 
1, '', "May include tabs, multiple lines" 
"that go on for a while", '', '', 
2, '', "More of the same" 

私はそれが次のようにほとんどの部分で動作するように取得しています:

first = pd.read_csv('file1.csv') 
second = pd.read_csv('file2.csv') 

merged = pd.concat([first, second], axis=0, ignore_index=True) 
merged.to_csv('test.csv') 

余分なlinがあるかのように見えますeをメッセージフィールドに追加すると、新しい行に分割されます。どうすればメッセージフィールドの新しい行に基づいて区切りを止めることができますか?

あなたはそれが改行文字 \n

に新しい行を開始しているように、それはあなたがseparatorlineterminator、または区切り文字のようなパラメータを変更してみてくださいfirst = pd.read_csv('file1.csv', delim_whitespace = True)

を使用してみてください可能性が見えた短い例から

+0

現在表示している結果を投稿すると便利です。 – sgrg

+0

@sgrg私の現在の出力を更新しました – Austin

+0

*ファイル2 *のようにデータが実際に破損していませんか? – Parfait

答えて

関連する問題