从系列词典中提取系列

Extract Series from Series of Dictionaries

我有一个 Pandas 系列对象,其中包含日期索引和字典值,如下所示:

timeSeries = pd.Series({'2014-05-01': {'property1': 1, 'property2': 2},
                        '2014-05-02': {'property1': 3, 'property2': 4}})

我知道每个字典都包含相同的键(property1property2)。有没有办法在没有循环的情况下得到一个系列,只有property1作为值。

即我要:

 propertySeries = pd.Series({'2014-05-01': 1,
                             '2014-05-02': 3})

你可以通过values and then use DataFrame构造函数将Series转换为numpy array

print (timeSeries.values.tolist())
[{'property1': 1, 'property2': 2}, {'property1': 3, 'property2': 4}]

df = pd.DataFrame(timeSeries.values.tolist(), index=timeSeries.index)
print (df)
            property1  property2
2014-05-01          1          2
2014-05-02          3          4

print (df['property1'])
2014-05-01    1
2014-05-02    3
Name: property1, dtype: int64

print (df['property2'])
2014-05-01    2
2014-05-02    4
Name: property2, dtype: int64

另一个较慢的解决方案:

print (timeSeries.apply(lambda x: x['property1'])) 
2014-05-01    1
2014-05-02    3
dtype: int64

print (timeSeries.apply(lambda x: x['property2'])) 
2014-05-01    2
2014-05-02    4
dtype: int64

如果您自己创建了时间序列,请使用 DataFrame.from_dict:

timeSeries = pd.DataFrame.from_dict({'2014-05-01': {'property1': 1, 'property2': 2},
                                     '2014-05-02': {'property1': 3, 'property2': 4}}, 
                                      orient='index')


print (timeSeries) 
            property1  property2
2014-05-01          1          2
2014-05-02          3          4

如果您自己创建了时间序列,则可以改为创建 DataFrame:

timeSeries = pd.DataFrame({'2014-05-01': {'property1': 1, 'property2': 2},
                           '2014-05-02': {'property1': 3, 'property2': 4}}).T
timeSeries['property1']
# 2014-05-01    1
# 2014-05-02    3
# Name: property1, dtype: int64