【发布时间】:2014-11-20 05:43:51
【问题描述】:
我有一个具有这种结构的字典列表。
{
'data' : [[year1, value1], [year2, value2], ... m entries],
'description' : string,
'end' : string,
'f' : string,
'lastHistoricalperiod' : string,
'name' : string,
'series_id' : string,
'start' : int,
'units' : string,
'unitsshort' : string,
'updated' : string
}
我想把它放在一个看起来像这样的 pandas DataFrame 中
year value updated (other dict keys ... )
0 2040 120.592468 2014-05-23T12:06:16-0400 other key-values
1 2039 120.189987 2014-05-23T12:06:16-0400 ...
2 other year-value pairs ...
...
n
其中 n = m* len(list with dictionaries)(其中“数据”中每个列表的长度 = m)
也就是说,'data' 中的每个元组都应该有自己的行。到目前为止我所做的是:
x = [list of dictionaries as described above]
# Create Empty Data Frame
output = pd.DataFrame()
# Loop through each dictionary in the list
for dictionary in x:
# Create a new DataFrame from the 2-D list alone.
data = dictionary['data']
y = pd.DataFrame(data, columns = ['year', 'value'])
# Loop through all the other dictionary key-value pairs and fill in values
for key in dictionary:
if key != 'data':
y[key] = dictionary[key]
# Concatenate most recent output with the dframe from this dictionary.
output = pd.concat([output_frame, y], ignore_index = True)
这看起来很hacky,我想知道是否有更“pythonic”的方式来做到这一点,或者至少这里是否有任何明显的加速。
【问题讨论】: