Professional Documents
Culture Documents
import csv
import numpy as np
import pandas as pd
datafram = pd.read_csv("players.csv")
datafram.head(10)
Out[1]:
surname team position minutes shots passes tackles saves
import csv
import matplotlib.pyplot as plt
File = open('PlayersExt.csv')
rows = csv.DictReader(File)
passesItems = []
minutesItems = []
colorsItems = []
for data in rows:
passesItems.append(float(data['passes']))
minutesItems.append(float(data['minutes']))
if 'goalkeeper' in data['position'] :
colorsItems.append('black')
elif 'defender' in data['position'] :
colorsItems.append('red')
elif 'midfielder' in data['position'] :
colorsItems.append('blue')
elif 'forward' in data['position'] :
colorsItems.append('grey')
else: colorsItems.append('brown')
plt.xlabel('minutes')
plt.ylabel('passes')
plt.scatter(minutesItems,passesItems, c=colorsItems,marker='o')
In [3]: # Create a pie chart showing the relative percentage of teams with 0, 1, and 2 red cards.
import csv
import matplotlib.pyplot as plt
File = open('Teams.csv')
rows = csv.DictReader(File)
Red_cards_0 = 0
Red_cards_1 = 0
Red_cards_2 = 0
for data in rows:
if int(data['redCards']) == 0 :
Red_cards_0=Red_cards_0+1
elif int(data['redCards']) == 1 :
Red_cards_1=Red_cards_1+1
elif int(data['redCards']) == 2 :
Red_cards_2=Red_cards_2+1
colors = ['yellow','red','brown']
plt.pie([Red_cards_0, Red_cards_1,Red_cards_2], labels=['redCards 0','redCards 1','redCards
2'],radius=1.8, colors=colors, autopct='%1.3f%%')
plt.show()
import csv
import matplotlib.pyplot as plt
File = open('Titanic.csv')
row = csv.DictReader(File)
bars = []
heights = []
First_class = 0
Second_class = 0
Third_class = 0
First_class_sum = float(0)
Second_class_sum = float(0)
Third_class_sum = float(0)
heights_1 = int(First_class_sum/First_class)
heights.append(heights_1)
heights_2 = int(Second_class_sum/Second_class)
heights.append(heights_2)
heights_3 = int(Third_class_sum/Third_class)
heights.append(heights_3)
surname = pd.read_csv("players.csv")
answer = surname[surname.team.str.contains('ia') & (surname.minutes < 200) & (surname.passes
>100) & surname.surname]
431 Kuzmanovic
Name: surname, dtype: object
In [6]: # What player on a team with "ia" in the team name played less than 200 minutes and made mor
e than 100 passes?
ds = pd.read_csv("players.csv")
an = ds[ds.team.str.contains('ia') & (ds.minutes < 200) & (ds.passes >100) & ds.surname]
431 Kuzmanovic
Name: surname, dtype: object
In [7]: # How many players on a team with ranking <10 played more than 350 minutes?
import csv
import numpy as np
info = open('PlayersExt.csv')
rows = csv.DictReader(info)
playersNumber = 0
for rf in rows:
if int(rf['ranking'])<10 and int(rf['minutes'])>350:
playersNumber = playersNumber+1
print(playersNumber)
54
In [8]: # Which team has the highest ratio of goalsFor to goalsAgainst? Print the team only.
OpenFile = open('Teams.csv')
row = csv.DictReader(OpenFile)
for j in row:
ratio = int(j['goalsFor'])/int(j['goalsAgainst'])
if ratio ==7:
print(j['team'])
Portugal
Married_Women = 0
while True:
OpenFile = open('Titanic.csv')
row = csv.DictReader(OpenFile)
Input_age = input('Enter Age: ')
if Input_age < "0":
break
Married_Women = 0
for i in row:
if 'Mrs.' in i['first'] and 'Cherbourg' in i['embarked'] and i['age']>Input_age:
Enter Age: 25
14 married women 25
Enter Age: 39
10 married women 39
Enter Age: 55
2 married women 55
Enter Age: 65
0 married women 65
Enter Age: 42
9 married women 42