How to choose specific values in a csv file with python

Question:

I need to be able to do the subtraction between the bigger and smaller positive number in ColumnB that have ColumnA=1,ColumnC=0. (I wrote them between ** to make it clearer)
I have something like this in my csv file but I have a lot more rows.

| Column A | Column B || Column C | Column D    |
| -------- | -------- || -------- | --------    |
| 1        |   -99    || 0        |    0.4567   |
| 1        |   -99    || 0        |     0.5678  |
| 1        | 60       || 40       |     0.123   |
| 1        | 67       || 60       |     0.2894  |
| 1        | **69**   || 0        |     0.3983  |
| 1        | 70       || 0        |     0.3983  |
| 1        | **71**   || 0        |     0.3983  | 
| 2        | -30      || 0        |     0.3983  | 
| 2        | -40      || 20       |     0.3983  | 
| 2        | 45       || 30       |     0.3983  | 
| 2        | 46       || 40       |     0.3983  | 

I tried to create a new column like this but I don’t have to do the mean I need to subtract the max with the min.

for u in range(1, 19):
        ColumnZ = df.query(f'ColumnB >0 & ColumnC == 0 & ColumnA == {u}')['ColumnB'].mean()
        test.loc[rowIndex, 'ColumnZ'] = ColumnZ
Asked By: Kkura

||

Answers:

cat subtract.csv
Column A,Column B,Column C,Column D
1,-99,0,0.4567
1,67,60,0.2894
1,69,0,0.3983
1,71,0,0.3983

import csv

with open('subtract.csv', 'r', newline='') as csv_file:
    dReader = csv.DictReader(csv_file)
    number_list = []
    for row in dReader:
        if int(row['Column A']) == 1 and int(row['Column C']) == 0 and int(row['Column B']) >= 0:
            number_list.append(int(row['Column B']) )
    new_val = max(number_list) - min(number_list)
new_val

2
Answered By: Adrian Klaver
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.