for循环构造与循环之前的数组

时间:2017-10-23 14:09:06

标签: python

在Python数据科学手册中给出了以下示例(倒数第二行是我不理解的,如图所示):

import pandas as pd
import numpy as np
import seaborn as sns
sns.set()

#Downloaded from: https://raw.githubusercontent.com/jakevdp/data-CDCbirths/master/births.csv

births = pd.read_csv('births.csv')
births['decades'] = (births['year'] // 10) * 10

# Rhobust sigma clipping operation - ignore this
quartiles = np.percentile(births['births'], [25, 50, 75])
mu = quartiles[1]
sig = 0.74 * (quartiles[2] - quartiles[0])
births = births.query('(births > @mu - 5 * @sig) & (births < @mu + 5 * @sig)')

births['day'] = births['day'].astype(int)

births.index = pd.to_datetime(10000 * births.year +
                             100 * births.month +
                             births.day, format='%Y%m%d')

births_by_date = births.pivot_table('births', [births.index.month, births.index.day])

#Help on the loop below
births_by_date.index = [pd.datetime(2012, month, day)
                       for (month, day) in births_by_date.index]

print(births_by_date.index)

我不理解for循环中births_by_date.index的构造。我知道循环正在应用于数据透视表,但我从未见过在循环之前放置的输出数组。

有人可以解释一下这是如何运作的,或者直接告诉我一个适当的解释吗?

我试过了: How do I save results of a "for" loop into a single variable?

这样的大量教程:https://www.learnpython.org/en/Loops

其他各种问题,但我找不到类似的东西。

2 个答案:

答案 0 :(得分:2)

它被称为&#34;列表理解&#34;您可以在其他来源中阅读here。评估理解,然后将其分配回数据框的索引,基本上为您的日期提供一年。它相当于:

some_list = []
for month, day in births_by_date.index:
    some_list.append(pd.datetime(2012, month, day))

births_by_date.index = some_list

答案 1 :(得分:1)

如上所述,这是列表理解。它是在列表上运行循环并通过转换生成另一个列表的简明语法。

将列表元素加倍的简单示例:

items = [1, 2, 3, 4]
doubled_items = [2*item for item in items]
# doubled_items is [2, 4, 6, 8]

这基本上与:

相同
items = [1, 2, 3, 4]
doubled_items = []

for item in items:
    doubled_items.append(2*item)