IamtryingtoaccomplishaseeminglysimpletaskinPython.ThisisnowmythirdSOpostinasmanydaysandIamembarassed.
Iamhopingtoopenacsvfile.Iwouldliketoloopthrougheachrow,andforeverycolumnineveryrow,ifthevalueisNOTa0ora1(itisa"?"),toessentiallyrewritethatvaluewiththevalueoftheverylastcolumn.Theverylastcolumncannevernotbea0or1.Itwillneverbe"?".Iamnotsomuchconcernedwiththeprintstatement,asIamreplacingthe"?"(ornon0/1)valuewiththevalueinthelastcolumn.
IhaveattachedanexamplecsvfileIamworkingwithhere:http://www.sharecsv.com/s/7bef636c33054cae624928297146bae1/house.csv
Ifyouareunabletoviewthelinkabove,IhavepastedaSAMPLEofthedatasetbelow:
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,?,1.00?,1.00,1.00,?,1.00,1.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.00,0.00,0.000.00,1.00,1.00,0.00,?,1.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,0.00,0.00,1.00,0.001.00,1.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,1.00,0.000.00,1.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,?,1.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,?,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,0.000.00,1.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,1.00,1.00,0.00,0.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,1.00,1.00,?,?,1.000.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,?,?,0.001.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,?,1.00,1.00,?,0.00,0.00,1.00,?,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,?,0.00,?,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,?,0.00,?,1.001.00,0.00,1.00,0.00,0.00,1.00,0.00,1.00,?,1.00,1.00,1.00,?,0.00,0.00,1.00,0.001.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,?,1.00,1.00,0.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,?,?,1.00,1.00,0.001.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,?,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,0.00,1.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,?,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,?,1.00,0.00,1.00,1.001.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,?,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,?,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,?,0.00,1.00,0.00,0.00,0.00,1.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,?,0.00,0.00,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,0.00,1.00,0.000.00,?,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,?,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,?,?,0.001.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,1.00,?,1.00,0.00,0.00,1.00,1.00,0.00,1.00,0.00,?,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,0.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,?,1.001.00,1.00,1.00,0.00,0.00,?,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,?,0.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,0.00,?,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,0.00,0.00,0.00,0.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,?,0.00,0.00,0.00,1.00,0.001.00,1.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,0.00,1.00,1.000.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,1.001.00,?,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,1.00,0.001.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,0.00,0.00,1.00,1.00,0.001.00,0.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,0.00,1.00,?,0.001.00,1.00,1.00,1.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,0.00,1.00,0.00,1.00,1.00
Hereismycurrentcodes:
import csvreader = csv.reader(open("house1.csv"), delimiter = ",")counter = 0for row in reader: # print("Opened Reader") currVal = row[:-1] counter = counter + 1 # print("set values") for column in row: questioncount = 0 # print("Looping columns") if (column != 0 or column != 1): questioncount = questioncount + 1 # This is where I should overwrite the value print("Row " + str(counter) + " has " + str(questioncount) + " question marks ")Idon"tunderstandwhyIamhavingsomuchdifficulty.Currently,theoutputofPycharmisthatitsayseverysinglerowandcolumnhasaquestionmark,whichisnottrue.
Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 1 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 2 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 3 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 4 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 5 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 6 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 7 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 8 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 9 has 1 question marks Row 435 has 1 question marks
Iwouldhopethatgivenalinelike
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,?,1.00,1.00,1.00,0.00,1.00,1.00
thispythonscriptwouldturnthatlineinto
0.00,1.00,0.00,1.00,1.00,1.00,0.00,0.00,0.00,1.00,1.00,1.00,1.00,1.00,0.00,1.00,1.00
Anyadvicewouldbeappreciated.
Yourcodehasgottwobugs.Thefirstoneisthatyoucheckifacolumndoesn"tequaltointegers0or1,butcolumncontainsastringvalue.Thesecondone,theinitializationofthequestioncountvariableandacallofprintfunctionshouldtakeplaceoutofthefor-loop.Hereisaworkingcode:
import csv
rows.append(row) print("Row {i} has {q} question marks".format(i=counter, q=questioncount))P.S.:I"veupdatedthecodesothatnowitsavesanotherfilewithquestionmarksbeingreplaced.
Hi,@constt.Thankyouforthis.Ijusttesteditandtheprintstatementworksgreat,awesome!However,asmyinitialquestionactuallyrequested,Icarenotsomuchfortheprintstatement,asIdoforbeingabletoreplaceaquestionmarkvaluewiththevalueinthelastcolumn.So,Iamunabletoselectyouasansweringthequestion.wundermahnFeb12"19at1:34Youwerecheckingifthestringvalues(fromcsvfile)"1.00"or"0.00"areequaltotheintegervalues(inyourifstatement)1or0.
Also,youshouldapply"not"tothetwochecksatonceotherwiseyourlogicfallsthrough.
You"rereadingthe.csv,butthecontentswillbereadasstrings,notasfloats,soyourcomparisonif(column!=0)willalwaysbetrue.Asaresult,youalwayscount1questionmark,whichyouthenproceedtoprintonceforeverycolumn,astheprintstatementisinthecolumnloop,nottherowloop.YouareassigningthelastvaluetocurrVal,butneverusingit.Sinceyourcodeislookingforquestionmarks,perhapsyoushouldnotcheckwhetheracolumnisn"t1or0,butwhetheritis"?"instead?
Yourcodewiththosefixes:
import csvreader = csv.reader(open("house1.csv"), delimiter=",")counter = 0for row in reader: counter = counter + 1 questioncount = 0 for column in row: if column == "?": questioncount += 1 print("Row " + str(counter) + " has " + str(questioncount) + " question marks ")Towritetheoutputyou"relookingfor:
import csvreader = csv.reader(open("simple.csv"), delimiter=",")writer = csv.writer(open("output.csv", "w", newline=""), delimiter=",")for row in reader: writer.writerow([column if column != "?" else row[-1:] for column in row])
interesting, thank you. Initially I tried looking for question marks but used ? and not ? . This definitely works, and is a clean solution (as always). Thank you for your suggestion. In a way of trying to go back to what my question was hopefully trying to ask, is how can I now write to the SAME csv file to replace those question marks with the value in the last column? That is what I was trying to achieve, just using print statements to test, as somebody suggested me to some days ago ;) wundermahn Feb 12 "19 at 1:40
I ve added that solution as well, keeping to your initial idea of opening the file and creating the reader and writer in one go. I would recommend using with if this code is part of a larger project. Note that you should get the same results regardless of using ? or ? . Grismar Feb 12 "19 at 1:46
Hi, @Grismar. Thank yo for this. Would you mind offering an explanation for writer.writerow([column if column != ? else 1.00 for column in row]) ? How does this grab the value of the last column in that row, doesn t this assume it is always 1? Also, this is note, just trying to create a suite of data cleaning tools and scripts I can use as I continue to practice with Python and Machine Learning. wundermahn Feb 12 "19 at 1:48
Oh I m sorry, I missed that aspect, you want ? to become equal to the last. Let me update the example. Note that it now writes row[-1:] for each ? it finds. Grismar Feb 12 "19 at 1:52
site design / logo 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2020.5.18.36843
StackOverflowworksbestwithJavaScriptenabled![]()