Reputation: 109
I have tried to make my program work, but as I don't have a lot of experience, I have faced a lot of problems. The idea of my work is that I analyze the data of the csv file and change the negative values to 0. So far I have managed to get all the data from the file and save it to a list, and manually assigning the "start" and "end" times of the file.
import csv
data = []
filename = str(input("Give the file name: "))
with open(filename) as csvfile:
spamreader = csv.reader(csvfile, delimiter=';')
for row in spamreader:
data.append(row)
print("File", filename, "is opened,", len(data),"rows will be analyzed.")
data.pop(0) #deletes the header
print (f'Analyzing data between dates {data[0][0]} and {data[1097][0]}.') #data[row][column]
How is it possible to change the negative values in the data (screenshot) to 0, and then save everything to a new csv file? Help would be massivly appreciated. screenshot inside the CSV file
[In the actual data file there is 1000+ rows]
Upvotes: 1
Views: 347
Reputation: 5434
If your file contains only numbers you can try the code below:
import csv
from collections import defaultdict
def convert(item):
try:
item = float(item)
if item < 0:
return 0
else:
return item
except ValueError:
return item
sums = defaultdict(list)
with open('Data.csv', 'r') as inp, open('output.csv', 'w', newline = '') as outp:
reader = csv.reader(inp, delimiter = ';')
writer = csv.writer(outp, delimiter = ';', dialect = 'excel')
headers = next(reader)
for line in reader:
line = [convert(i) for i in line]
sums[line[0]].append(line[1])
writer.writerow(line)
for k,v in sums.items():
print('key: {} total: {}'.format(k,sum(v)))
Firstly you can use the with
context manager to open both files that you are going to read and write, from and to respectively. Then instead of storing it into a list, you can transform your data and directly write it to your target file, here named output.csv
. The for line in reader
here loops through the lines and transforms each value into an absolute integer before writing it to the file.
I added the defaultdict
from the Python standard library if you want to store the values into a dictionary to use them. The dictionary sums
will include a key by date and its values. To print the sum by days, you can simply use a string format to print the key and the summed values.
Upvotes: 1
Reputation: 2175
This is very easily achieved using a pandas DataFrame:
import pandas as pd
c = pd.read_csv('inputcsvfile.csv')
processed_csv = c[c > 0].fillna(value=0)
processed_csv.to_csv('outputcsvfile.csv')
Pandas is an external library so you would need to install it (pip install pandas
) before you can use this solution
Upvotes: 0