1

I have two csv file like test1.csv, test2.csv and in them some column headers aren't repeated and I need to append second unrepeated column to end of first and merge it to 'merge.csv' when my program appends it to first. Here is my code :

import os
import glob
import pandas as pd
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
combined_csv = pd.concat([pd.read_csv(f, sep = ';', encoding = 'ANSI', quotechar = '"') for f in all_filenames])
combined_csv.to_csv("merged.csv", index=False, sep = ';', encoding = 'ANSI')

First csv data1.csv is like :

id;name;surname
23764;olzhas;bermaganbetov
23001;zhanik;kalieva

Second one data2.csv is like :

id;name;country
23732;almas;almaty
23001;aierke;astana

Here is the result: merged.csv

country;id;name;surname
;23764;olzhas;bermaganbetov
;23001;zhanik;kalieva
almaty;23732;almas;
astana;23001;aierke;

when I need something like:

id;name;surname;country
23764;olzhas;bermaganbetov;
23001;zhanik;kalieva;
23732;almas;almaty
23001;aierke;astana
tripleee
  • 175,061
  • 34
  • 275
  • 318
Olya
  • 11
  • 1
  • 1
    this `23001;aierke;astana` should be like `23001;aierke;;astana`, in your expected output? – Nihal Mar 26 '19 at 06:14
  • use `combined_csv = combined_csv[['id','name','surname','country']]` after your `pd.concat` – Nihal Mar 26 '19 at 06:15

0 Answers0