项目作者: PyladiesHoChiMinh

项目描述 :
Learn about Dictionary and start with Pandas
高级语言: Jupyter Notebook
项目地址: git://github.com/PyladiesHoChiMinh/workshop4.git
创建时间: 2018-03-29T03:28:08Z
项目社区:https://github.com/PyladiesHoChiMinh/workshop4

开源协议:

下载


Learn about Dictionary and start with Pandas

Long time no see,

In this session, we will walk you through through a learning session of how to use dictionary and get started with pandas, one of the most popular open-source library in python.

Before jumping into content, we would like to thank Eric Matthews for his amazing book “Python Crash Course: A Hands-on, Project-based Introduction to Programming”
Again, feel free to ask if you have any question.

Outline

  • What is Dictionary
    • Accessing values
    • Adding/ Removing new key-value pairs
    • Starting with values in a Dictionary
  • How to work with Dictionary
    • Looping
      • Loop through all key-value pairs
      • Loop through all the keys in a Dictionary
      • Loop through all a Dictionary’s keys in order
      • Loop through all values in a Dictionary
    • Nesting
      • A list of Dictionary
      • A list of a Dictionary
      • A Dictionary in a Dictionary

What is a Dictionary?

Let us take a simple to explain what Dictionary or Dict is

  1. """
  2. Example of a Dictionary with a variable named `workshop_attendee`.
  3. """
  4. workshop_attendee = {"Name": ["Alice", "Bob", "Chris"], "Occupation": ["student", "marketer", "researcher"], "Age": [20, 27, 35]}
  5. print(workshop_attendee)

In simple words, we would say Dict is a data structure allowing us to

How to access values
  1. print(workshop_attendee["student"])
  2. print(workshop_attendee["student"][0])
  1. # Return an object Dictionary_values, e.g. dict_values([key1, key2])
  2. workshop_attendee.values()
  1. # Return an object of Dictionary keys, e.g. dict_keys([key1, key2])
  2. workshop_attendee.keys()
  1. # Return an object of Dictionary item, e.g. dict_items([(key1, value1), (key2, value2)])
  2. workshop_attendee.items()

Why dict_items, dict_values, dict_values?

How to add/ remove new key-value pairs
  1. workshop_attendee["Year of Experience"] = "Less than a year" #Add a value.
  2. print(workshop_attendee)
  3. del workshop_attendee["Year of Experience"] #Remove keys and values.
  4. print(workshop_attendee)

How to work with Dictionary

  1. for key, value in workshop_attendee.items():
  2. print("\n Key %s" % key)
  3. print("\n Value %s" % value)