0

I want to add one names column in my file (modifiedFlights.csv) from another file (original.csv) which has a column names. The goal is to add names in modifiedFlights.csv after comparing column hashes which is present in both files. But I am not able to do so.

import os
import glob
from pathlib import Path
import pandas as pd
import pandas
import csv
import numpy as np
from pandas import DataFrame
import sys, argparse, csv

hashes=pd.read_csv(r'C:\Users\Sajid\Desktop\original.csv', usecols=[0]) #hashes in original.csv
names=pd.read_csv(r'C:\Users\Sajid\Desktop\original.csv', usecols=[1])
this=pd.read_csv(r'C:\Users\Sajid\Desktop\csv files\modifiedFlights.csv', usecols=[4])# hashes in modifiedFlights.csv

for i in hashes:
    for y in this:
        if i == y:
            results_row=pd.read_csv(r'C:\Users\Sajid\Desktop\original.csv', usecols=[1], userows=[i])
            with open(r'C:\Users\Sajid\Desktop\csv files\modifiedFlights.csv','r') as csvinput:
                with open(r'C:\Users\Sajid\Desktop\out.csv', 'w') as csvoutput:
                    writer = csv.writer(csvoutput)
                    for row in csv.reader(csvinput):
                        writer.writerow(row+[result_row])

Sebastien D
  • 4,369
  • 4
  • 18
  • 46
  • Heard of `pd.merge()`? This could lead you to a solution. – Sebastien D Jun 22 '19 at 00:00
  • See also https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples to improve your question – Valentino Jun 22 '19 at 00:05
  • I guess what you need was called "left join" or "join". It is a basic operation of dataframe work. You probably could directly find a function from whatever package you were using by searching in the manual. – Jason Jun 22 '19 at 00:14

0 Answers0