[英]Structure series from a dictionary (python)
I currently have the following dictionary: 我目前有以下字典:
"2019-09-30": {
"date": "2019-09-30",
"period": "+1y",
"growth": "0.1540",
"earningsEstimateAvg": "13.2600",
"earningsEstimateLow": "11.5200",
},
"2018-09-30": {
"date": "2018-09-30",
"period": "+1q",
"growth": "0.2800",
"earningsEstimateAvg": "2.6500",
"earningsEstimateLow": "2.4300",
},
"2018-06-30": {
"date": "2018-06-30",
"period": "+1q",
"growth": "0.2930",
"earningsEstimateAvg": "2.1600",
"earningsEstimateLow": "1.8000",
},
I have defined the dictionary as a variable ('market_data') and I would like to extract the respective series (with each time the date). 我已将字典定义为变量('market_data'),我想提取相应的系列(每次带有日期)。
For instance, the growth variable would include: ("2019-09-30" .1540, "2018-09-30" .2800, "2018-06-30 .2930) 例如,增长变量将包括:(“ 2019-09-30” .1540,“ 2018-09-30” .2800,“ 2018-06-30 .2930)
while earningsEstimateAvg would include: ("2019-09-30" 13.2600, "2018-09-30" 2.6500, "2018-06-30 2.1600) 而incomeEstimateAvg将包括:(“ 2019-09-30” 13.2600,“ 2018-09-30” 2.6500,“ 2018-06-30 2.1600”)
I have tried to play with DataFrame but do not manage to get the proper format. 我尝试使用DataFrame,但无法获得正确的格式。
On a second step, I would like to be able to call that list (which will include more than 3 dates) and return all growth rate between two user-defined dates. 第二步,我希望能够调用该列表(将包含3个以上的日期)并返回两个用户定义的日期之间的所有增长率。
Thanks a lot for your help guys, really much appreciated! 非常感谢您的帮助,非常感谢! :)
:)
Max 最高
Try this: 尝试这个:
df = pd.DataFrame(list(market_data.values()))
Output: 输出:
date earningsEstimateAvg earningsEstimateLow growth period
0 2019-09-30 13.2600 11.5200 0.1540 +1y
1 2018-09-30 2.6500 2.4300 0.2800 +1q
2 2018-06-30 2.1600 1.8000 0.2930 +1q
To get data in the format you want, you can use this: 要获取所需格式的数据,可以使用以下命令:
df[['date', 'growth']].set_index('date').to_dict()
df[['date', 'earningsEstimateAvg']].set_index('date').to_dict()
Output: 输出:
{'growth': {'2018-06-30': '0.2930',
'2018-09-30': '0.2800',
'2019-09-30': '0.1540'}}
{'earningsEstimateAvg': {'2018-06-30': '2.1600',
'2018-09-30': '2.6500',
'2019-09-30': '13.2600'}}
To get data between two dates: 要获取两个日期之间的数据:
mask = (df['date'] > start_date) & (df['date'] <= end_date)
df.loc[mask]
First, I would go over your dictionary, that holds all data and flatten it: 首先,我将翻阅您的字典,其中包含所有数据并将其展平:
my_array = [data for key, data in my_dictionary.items()]
Then I would create the dictionary you need, eg: 然后,我将创建所需的字典,例如:
growth = {x['date']: x['growth'] for x in my_array}
If you need a list of dictionaries: 如果您需要词典列表:
growth = [{"date": x['date'], "growth": x['growth']} for x in my_array]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.