Python Pandas CSV

Asked

Viewed 785 times

2

I started to learn Python not long ago and I’m doing a project to normalize customer data. But I don’t know how to make a comparison like: read on CSV field CP7, search for all CP7 of CSV CTT and write on the table Pnorm, If you find only one address, write on CSV Norm, find more than 1 continue to analyze in other fields.

import csv
import pandas as pd
import numpy as np

cp7 = []

cp4 = []

local = []

tv = []

nr = []

mor = []

nll = sum(1 for line in open('example.csv'))  # count the number of lines
nl = nll - 1

df1 = pd.read_csv("example.csv", names=['CP7', 'CP4', 'Local', 'TV', 'NR'])
df2 = pd.read_csv("CTT.csv", names=['CP7', 'CP4', 'Local', 'TV', 'NR'])
pn = pd.read_csv("PNorm.csv", names=['CP7', 'CP4', 'Local', 'TV', 'NR'])

for row in range(nl):
    if cp7 is True:

    # compare column with another csv file

        if cp7 == 1:  # cp7 matches with only one address
            File = open('Norm.csv', 'w')
            Norm = csv.writer(File)
            Norm = [row for col in Norm]
            File.close()
        else:  # all cp7 possibilities
            File = open('PNorm.csv', 'w')
            PNorm = csv.writer(File)
            PNorm = [row for col in PNorm]
            File.close()

    elif cp4 is True:  # all cp4 possibilities

    # compare column with another csv file

        File = open('PNorm.csv', 'w')
        PNorm = csv.writer(File)
        PNorm = [row for col in PNorm]
        File.close()
    else:
        pass

    if local is True:  # all local possibilities

    # read local

        File = open('PNorm.csv', 'w')
        PNorm = csv.writer(File)
        PNorm = [row for col in PNorm]
        File.close()
    else:
        pass

    if tv is True: # tipo de via

    # compare column with another csv file

        if tv == 1:  # TipVia matches with only one address
            File = open('Norm.csv', 'w')
            Norm = csv.writer(File)
            Norm = [row for col in Norm]
            File.close()
        else:  # all TipVia possibilities
            File = open('PNorm.csv', 'w')
            PNorm = csv.writer(File)
            PNorm = [row for col in PNorm]
            File.close()
    else:
        pass

    if nr is True:  # nome de rua e numero policia

    # compare column with another csv file

        if nr == 1:  # NomeRua_NumPol matches with only one address
            File = open('Norm.csv', 'w')
            Norm = csv.writer(File)
            Norm = [row for col in Norm]
            File.close()
        else:  # all possibilities
            File = open('PNorm.csv', 'w')
            PNorm = csv.writer(File)
            PNorm = [row for col in PNorm]
            File.close()
    else:
        pass
  • Hello John, welcome to Stack Overflow! Have you solved this question? I was a little confused as to what you want in this code - can you explain it better? Do you want to search for a data in one file and save the data found in another? If you can, write an example of how the program would run.

  • And if you can, include data dummy example that the program would have to search.

  • The code I’m trying to do is to compare addresses, and these are separated into different columns, each row is an address and compares to another file where it contains all the correct addresses.

  • What differs a correct address from an incorrect one? Is CP7 an address? Or an element of the address? Or neither? The elements of one file are exactly what is found in the other, or they will have accent/high/low box difference, etc.?

  • What I understood: You will look for "Rua do Rosário 10" in a file that has lines like: "Avenida Duque de Ávila 20", "Rua Doutor José Espírito Santo 30". If it’s not, please edit your question and include snippets of files with example of what you intend to do.

  • An address is composed of CP7 or CP4, typeVia, Locality, Street name and police number, will not have any accent. What happens and I can receive addresses with errors or incomplete.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.