In this Python Dictionaries article we are going to learn about Python Dictionaries for Data Analysis and Visualization, Python is powerful language for data analysis and visualization. and one of the key data structures in Python is dictionary, which is useful for working with data sets. dictionary is a collection of key value pairs, where each key is unique and associated with a value. Dictionaries are implemented using hash tables, which provide fast access to values based on their keys. and this makes dictionaries an efficient data structure for tasks such as counting, grouping and aggregating data.
So for creating a dictionary in Python, we can use curly braces {} and separate key value pairs with colons. this is an example of a dictionary that maps few countries to their populations.
1 2 3 4 5 6 7 |
populations = { "China": 1444216107, "India": 1381380962, "United States": 331449281, "Indonesia": 274021604, "Pakistan": 225200000 } |
Now we have created the dictionary, for accessing a value in dictionary, we use the key as the index.
1 2 3 4 5 6 7 8 9 |
populations = { "China": 1444216107, "India": 1381380962, "United States": 331449281, "Indonesia": 274021604, "Pakistan": 225200000 } china_population = populations["India"] print(china_population) |
If you run the code this will be the result
Also you can iterate over Python Dictionaries, for iterating over a dictionary we can use for loop. for loop will iterate over keys and we can access the values using keys.
1 2 3 4 5 6 7 8 9 10 |
populations = { "China": 1444216107, "India": 1381380962, "United States": 331449281, "Indonesia": 274021604, "Pakistan": 225200000 } for country in populations: population = populations[country] print(country + ": " + str(population)) |
Run the code and this will be the output
Dictionaries are useful for data analysis tasks such as counting, grouping and aggregating data. let’s look at a few examples.
Counting Occurrences of Values in a List
To count the occurrences of values in a list, we can use a dictionary to keep track of the counts. this is an example that counts the number of times each fruit appears in list:
1 2 3 4 5 6 7 8 9 10 |
fruits = ["apple", "banana", "orange", "apple", "banana", "apple"] fruit_counts = {} for fruit in fruits: if fruit in fruit_counts: fruit_counts[fruit] += 1 else: fruit_counts[fruit] = 1 print(fruit_counts) |
This will be the result
Grouping Data by a Key
For grouping data by a key, we can use dictionary to store data for each key. this is an example that groups a list of students by their major:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
students = [ {"name": "John", "major": "Math"}, {"name": "Bob", "major": "Computer Science"}, {"name": "GeeksCoders", "major": "Math"}, {"name": "David", "major": "Computer Science"} ] students_by_major = {} for student in students: major = student["major"] if major in students_by_major: students_by_major[major].append(student) else: students_by_major[major] = [student] print(students_by_major) |