Reputation: 969
I am trying to calculate selling prices on all the values of row[7] in a csv for all the customers whose discount % are stored in the list Y_pred. For first customer it calculates correctly. For the next customer I initialize the list containing the resultant selling price, named selling_price, as 0. Then it calculates selling prices for rest of all customers as 0.
rows = csv.reader(open('sample_data_ml.csv', 'r'))
newrows = []
count = 0
Y_pred = np.asarray(Y_pred, dtype='float64')
for margin in Y_pred:
selling_price = []
print(margin)
for row in rows:
#print(row)
if count == 0:
#newrows.append(row)
count = count+1
else:
try:
row[7] = float(row[7])
#print(row[7])
except ValueError as e:
#print(row[7])
row[7] = 0
finally:
print(row[7])
sell = row[7] + margin*row[7]
selling_price.append(sell)
print(selling_price)
print('-'*60)
print('-'*60)
Output:
[array([312.81321038]), array([223.43800741]), array([1489.58671609]), array([49.34255997]), array([726.17352409]), array([2583.50196071]), array([921.68178058]), array([335.15701112]), array([1885.25818755]), array([1070.64045219]), array([265.3326338]), array([451.53097331]), array([223.43800741]), array([1978.3573573]), array([512.04543365]), array([679.62393921]), array([223.43800741]), array([195.50825649]), array([96.82313655]), array([65.16941883]), array([242.05784136]), array([176.88842254]), array([512.04543365]), array([553.94006004]), array([256.02271683]), array([256.02271683]), array([269.98759229]), array([381.706596])]
------------------------------------------------------------
[0.07486867]
[]
------------------------------------------------------------
[0.12372819]
[]
------------------------------------------------------------
[0.11737926]
[]
------------------------------------------------------------
[0.11570468]
[]
------------------------------------------------------------
[0.09456172]
[]
------------------------------------------------------------
[0.09490446]
[]
------------------------------------------------------------
------------------------------------------------------------
Edit:
print(Y_pred)
Output:
[[0.09528435]
[0.07486867]
[0.12372819]
[0.11737926]
[0.11570468]
[0.09456172]
[0.09490446]]
`print(row)` (printed one sample row)
output:
['4311', '', 'Prawns-A Grade (31/40)', 'Smerkato Certified', '1', 'KGS', '410', '348.5']
Upvotes: 1
Views: 64
Reputation: 149085
Here is the culprit:
rows = csv.reader(open('sample_data_ml.csv', 'r'))
...
for margin in Y_pred:
...
for row in rows:
The csv.reader
is a single pass iterator. After first iteration of the outer loop, it has reached the end of file and the inner loop will be immediately terminated.
Two possible ways here:
reset the reader inside the outer loop:
...
for margin in Y_pred:
...
rows = csv.reader(open('sample_data_ml.csv', 'r'))
for row in rows:
you will re-read the csv file for each outer iteration
save the rows in a list:
rows = list(csv.reader(open('sample_data_ml.csv', 'r')))
...
for margin in Y_pred:
...
for row in rows:
you read the file only once, but waste some memory for the list
Upvotes: 1