-4

I have a csv file where i have datas like

reviews                                                                                                                          pos_neg
men hecne bawa duwmedim bu kitabdan. yeqin wie sunni ayrimi etmisiz men wieyem dede babadan bildiyim dualar bu kitabda bawqadi  Negative
Cavidanquluzade_official instagram seyfem xeber pırogramı                                                                       Negative
səhər və axşam zikrlərində 108 ci zikrin başlığının latınca yazılışı verilmiyib.                                                Positive
Bir müslümana aid herşey var                                                                                                    Positive

now i need to change all "positive" to "1" and all negatives to "0". Could you please help me?

  • 2
    Use the `csv` module to open the file, loop through it, change the value with a simple `if`, then write it back to a file using the `csv` module…!? – deceze Sep 01 '20 at 08:12
  • Your CSV currently does not show commas, making it a not really a `comma separated values` document. Is there anything you already tried? – francis duvivier Sep 01 '20 at 08:12
  • Use pandas to read the csv and the map the column with pos to 1 and neg to 0. – Trenton McKinney Sep 01 '20 at 08:12
  • This seems like a pretty basic task, doable with just the standard library ``csv`` module. What exactly is your problem doing what you want? What have you tried so far? What specific technical problem have you encountered? – MisterMiyagi Sep 01 '20 at 08:13
  • i just need to do it as fast as i can do, with 2 for loops it will take a lot – Samed Mukush Sep 01 '20 at 08:14
  • So how long *does* it (what?) take now, and how long *should* it take? Do you actually have to interpret this as CSV, or is a plain text replacement of ``Negative``/``Positive`` fine? – MisterMiyagi Sep 01 '20 at 08:16
  • [Remap values in pandas column with a dict](https://stackoverflow.com/questions/20250771/). Pandas will also make it easy to perform other vectorized text cleaning and analysis operations. – Trenton McKinney Sep 01 '20 at 08:16

1 Answers1

-2

If I would write that kind of utility, I'd do something like this:

import argparse
import csv
import sys

parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType())
args = parser.parse_args()

out = csv.DictWriter(sys.stdout, ('review', 'pos_neg'))
out.writeheader()

for row in csv.DictReader(args.file):
    row['pos_neg'] = ['Negative', 'Positive'].index(row['pos_neg'])
    out.writerow(row)

Can be used like this on the command line:

$ python converter.py the_file.csv > the_new_file.csv

You may need to fiddle with the options for the CSV reader, as it appears you have a TSV or fix-width file perhaps…?

deceze
  • 510,633
  • 85
  • 743
  • 889