Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data Dictionaries in Python
#1
Hello,

Hope you are well.

I want to load the all time premier league table in to a a data dictionary in phyton, https://en.wikipedia.org/wiki/Premier_Le...statistics

I have the table in that link in a csv file. PL.csv.

I want to load each column in to a data dictionary using the first entry in each row as the row lable.

How can I do this? I know this is basic stuff but I am new to the language.

Regards,
Aidan.
Reply
#2
Please post a small data sample (just a few rows)
Reply
#3
(Nov-24-2019, 03:35 AM)Larz60+ Wrote: Please post a small data sample (just a few rows)

Thank you for your reply.

Pos,Club,Seasons,Pld,Win,Draw,Loss,GF,GA,GD,Pts
1,Manchester United,27,1038,648,224,166,1989,929,1060,2168
2,Arsenal,27,1038,565,260,213,1845,1013,832,1955
3,Chelsea,27,1038,558,257,223,1770,1002,768,1931
4,Liverpool,27,1038,529,262,247,1774,1046,728,1849

This is just a historic list of the performace of teams in a league, the first row is the column labels.

I am looking to load this data in to a data dictioary in python.
Reply
#4
The csv module can do this for you.

import csv


result = []
with open('PL.csv') as fd:
    reader = csv.DictReader(fd)
    for row in reader:
        result.append(row)


print(result)
You get a list with dicts. The headers are included as keys in the dicts.


https://docs.python.org/3/library/csv.ht...DictReader

If you scroll down, you see some other examples.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Nov-24-2019, 10:32 AM)DeaD_EyE Wrote: The csv module can do this for you.

import csv


result = []
with open('PL.csv') as fd:
    reader = csv.DictReader(fd)
    for row in reader:
        result.append(row)


print(result)
You get a list with dicts. The headers are included as keys in the dicts.


https://docs.python.org/3/library/csv.ht...DictReader

If you scroll down, you see some other examples.

Thank you for your reply.

I am looking to import the csv file into columns in a dictionary.

Your code above loads the csv data in and labels it correctly etc.

Is the data already in columns with this code and if so how do I access individual items lke the 5th best team etc?
Reply
#6
(Nov-24-2019, 10:50 AM)mrsenorchuck Wrote: Is the data already in columns with this code and if so how do I access individual items lke the 5th best team etc?
Can show a way with Pandas which has a lot of power if you shall manipulate data for different results.
Can take table direct from web-site,to a DataFrame with pd.read_html.
Here a Notebook,the table is pretty clean and can start to work with right away for different result.

As you see in Notebook example can ask question that check for more than one result.
# More than 3 1st and also more than 560 Win
df[(df['1st'] >= 3) & (df['Win'] > 560)]
Reply
#7
(Nov-24-2019, 02:07 PM)snippsat Wrote:
(Nov-24-2019, 10:50 AM)mrsenorchuck Wrote: Is the data already in columns with this code and if so how do I access individual items lke the 5th best team etc?
Can show a way with Pandas which has a lot of power if you shall manipulate data for different results.
Can take table direct from web-site,to a DataFrame with pd.read_html.
Here a Notebook,the table is pretty clean and can start to work with right away for different result.

As you see in Notebook example can ask question that check for more than one result.
# More than 3 1st and also more than 560 Win
df[(df['1st'] >= 3) & (df['Win'] > 560)]


Thanks for your reply.

You see I can't use pandas.

Looking for a good worked example importing and interogating data using dictionaries
Reply
#8
Working example:

from csv import DictReader

with open('premier_league.txt', 'r') as f:
    data = list(DictReader(f))
data is list of dictionaries. We can 'query' like this:

>>> [row['Club'] for row in data if row['Pos'] == '4']
['Liverpool']
>>> next(row['Club'] for row in data if row['Pos'] == '4')
'Liverpool'
>>> (max((row for row in data), key=lambda x: int(x['Seasons'])))['Club']  # Club with max number of seasons
'Manchester United'
As you notice, DictReader will not convert numbers into integers automagically. It can be done 'manually' during import or after.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(Nov-24-2019, 02:49 PM)perfringo Wrote: Working example:

from csv import DictReader

with open('premier_league.txt', 'r') as f:
    data = list(DictReader(f))
data is list of dictionaries. We can 'query' like this:

>>> [row['Club'] for row in data if row['Pos'] == '4']
['Liverpool']
>>> next(row['Club'] for row in data if row['Pos'] == '4')
'Liverpool'
>>> (max((row for row in data), key=lambda x: int(x['Seasons'])))['Club']  # Club with max number of seasons
'Manchester United'
As you notice, DictReader will not convert numbers into integers automagically. It can be done 'manually' during import or after.

Thanks for this detail.

Ok, so I see you are using specific filter criteria to query specific rows.

When the data is loaded in to the data dictionary is it easy to sum columns etc? I am guessing they would have to be converted to int first?
Reply
#10
So I used this appraoch below:

How can I get the average points and pick teams that have finished first?

Sample data at the very bottom

# Purpose: Inputing data from a csv file
# Example of: File input from a csv file, using a dictionary

print("This program loads all historic premier league data")

# start with an empty dictionary
# dictionary keys will be the (Pos, Club)
premier = {}

print()
print("Historic premier league")
# open the file
with open(r"Historic_PL.csv") as data_file:
# read in the first line containing the headers
headers = data_file.readline()

# for each other line in the file
for line in data_file:
# split each line into components (remove white space from ends of line)
Pos,Club,Seasons,Pld,Win,Draw,Loss,GF,GA,GD,Pts,First,Second,Third,Fourth,Relegated,Best = line.strip().split(",")

# insert the data into the dictionary
premier[(int(Pos), Club)] = (int(Seasons),int(Pld),int(Win),int(Draw),int(Loss),int(GF),int(GA),int(GD),int(Pts),int(First),int(Second),int(Third),int(Fourth),int(Relegated),int(Best))

print(f"Number of values: {len(premier)}")



################################################################
Pos,Club,Seasons,Pld,Win,Draw,Loss,GF,GA,GD,Pts,First,Second,Third,Fourth,Relegated,Best
1,Manchester United,27,1038,648,224,166,1989,929,1060,2168,13,6,3,1,0,1
2,Arsenal,27,1038,565,260,213,1845,1013,832,1955,3,6,5,7,0,1
3,Chelsea,27,1038,558,257,223,1770,1002,768,1931,5,4,5,2,0,1
4,Liverpool,27,1038,529,262,247,1774,1046,728,1849,0,4,5,7,0,2
5,Tottenham Hotspur,27,1038,446,257,335,1547,1306,241,1595,0,1,2,3,0,2
6,Everton,27,1038,377,296,365,1357,1311,46,1427,0,0,0,1,0,4
7,Manchester City,22,848,391,196,261,1374,975,399,1369,4,2,2,1,2,1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I save Python dictionaries in Matlab? jlostinco 1 2,821 Jul-04-2019, 11:35 PM
Last Post: scidam
  creating an 'adress book' in python using dictionaries? apollo 6 14,796 May-06-2019, 12:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020