0
I’m making a program to perform some mathematical operations inside a CSV file, but I would not like to use pandas, I wanted to do it through a dictionary. Follow the code I’m doing to perform the summation function.
import csv
soma_public = ['1930', '1950', '1970', '1990', '2010']
with open('WorldCups.csv') as csv_file:
soma = sum(int(vals[2]) for vals in csv.reader(csv_file) if vals[0] in soma_public)
print(soma)
This is my CSV data:
Year,Country,Winner,Runners-Up,Third,Fourth,GoalsScored,QualifiedTeams,MatchesPlayed,Attendance
1930,Uruguay,Uruguay,Argentina,USA,Yugoslavia,70,13,18,590.549
1934,Italy,Italy,Czechoslovakia,Germany,Austria,70,16,17,363.000
1938,France,Italy,Hungary,Brazil,Sweden,84,15,18,375.700
1950,Brazil,Uruguay,Brazil,Sweden,Spain,88,13,22,1.045.246
1954,Switzerland,Germany FR,Hungary,Austria,Uruguay,140,16,26,768.607
1958,Sweden,Brazil,Sweden,France,Germany FR,126,16,35,819.810
1962,Chile,Brazil,Czechoslovakia,Chile,Yugoslavia,89,16,32,893.172
1966,England,England,Germany FR,Portugal,Soviet Union,89,16,32,1.563.135
1970,Mexico,Brazil,Italy,Germany FR,Uruguay,95,16,32,1.603.975
1974,Germany,Germany FR,Netherlands,Poland,Brazil,97,16,38,1.865.753
1978,Argentina,Argentina,Netherlands,Brazil,Italy,102,16,38,1.545.791
1982,Spain,Italy,Germany FR,Poland,France,146,24,52,2.109.723
1986,Mexico,Argentina,Germany FR,France,Belgium,132,24,52,2.394.031
1990,Italy,Germany FR,Argentina,Italy,England,115,24,52,2.516.215
1994,USA,Brazil,Italy,Sweden,Bulgaria,141,24,52,3.587.538
1998,France,France,Brazil,Croatia,Netherlands,171,32,64,2.785.100
2002,Korea/Japan,Brazil,Germany,Turkey,Korea Republic,161,32,64,2.705.197
2006,Germany,Italy,France,Germany,Portugal,147,32,64,3.359.439
2010,South Africa,Spain,Netherlands,Germany,Uruguay,145,32,64,3.178.856
2014,Brazil,Germany,Argentina,Netherlands,Brazil,171,32,64,3.386.810
I need to do the following operations:
Sum of Crown Audiences with Final Years 0 (1930, 1950, etc)
Total number of goals between the 1954 and 1990 cups, inclusive
Audience average
Average goals per match
Number of times the host country was champion
Number of times the Brazilian team was among the top 4 positions
Year of the editions in which the team of France finished in third place
Number of wins per country, ranked in increasing order of number of titles
What is your doubt for what you need to do?
– João Victor
I need to make a program to meet the above requirements, these data are in a CSV file that I posted together. In short...I need to understand how to perform these operations
– Pedro Paes Siniscalchi
Basically you can make a loop for and go storing the value of each line you want based on a condition. Similar to what you did for the sum. It just doesn’t have to be all on the same line. It makes a loop even block for.
– João Victor
Just need to know what you need to calculate what you want and store that information during the loop. Ex.: The average you need the amount of elements and the sum. Then just store the sum and at the end divide by the number of times you added up.
– João Victor