2_chapter1.pdf
2_chapter2.pdf
2_chapter3.pdf
2_chapter4.pdf

4. chapter 4笔记

  • enumerate

    1. fam = [1.73, 1.68, 1.71, 1.89]
    2. for index, height in enumerate(fam):
    3. print("index " + str(index) + ": " + str(height))
  • 字典遍历:items()

    world = { "afghanistan":30.55, "albania":2.77, "algeria":39.21 } 
    for key, value in world.items(): 
      print(key + " -- " + str(value))
    
  • numpy arrays:

(1) 一维array

import numpy as np 
np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79]) 
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7]) 
bmi = np_weight / np_height ** 2 
for val in bmi: 
    print(val)

(2) 二维array:np.nditer() —- 循环迭代numpy数组各元素

import numpy as np 
np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7]) 
meas = np.array([np_height, np_weight])
for val in np.nditer(meas):
    print(val)
  • DataFrame循环遍历行:iterrows()

    import pandas as pd 
    data = {
      "country": ["Brazil", "Russia", "India", "China", "South Africa"],
      "capital": ["Brasilia", "Moscow", "New Delhi", "Beijing", "Pretoria"],
      "area": [8.516, 17.100, 3.286, 9.597, 1.221],
      "population": [200.40, 143.50, 1252.00, 1357.00, 52.98]
    }
    brics = pd.DataFrame(data, index = ["BR","RU","IN","CH","SA"])
    for lab, row in brics.iterrows(): 
      print(lab + ": " + row["capital"])
    
  • 增加汇总列 ```r

    1. 循环法

    for lab, row in brics.iterrows():

    - Creating Series on every iteration

    brics.loc[lab, “name_length”] = len(row[“country”]) print(brics)

2. apply()

brics[“name_length”] = brics[“country”].apply(len) ``` 2_chapter5.pdf