[英]Iterate Through Nested Dictionary to Create Dataframe and Add New Column Value
Python noob so bear with me. Python noob 所以请耐心等待。
I have a list of a dictionary of stock info.我有一个股票信息字典的列表。 Variable name "json", I want to convert it to a dataframe then append a column with the ticker symbol in a new column next to the data.变量名称“json”,我想将其转换为 dataframe 然后 append 一个列,其中包含数据旁边的新列中的股票代码。 See below.见下文。
json =
[{'Meta Data': {'1. Information': 'Monthly Prices (open, high, low, close) and Volumes', '2.
Symbol': 'AAPL', '3. Last Refreshed': '2021-01-29', '4. Time Zone': 'US/Eastern'}, 'Monthly Time
Series': {'2021-01-29': {'1. open': '133.5200', '2. high': '145.0900', '3. low': '126.3820', '4.
close': '131.9600', '5. volume': '2239366098'}, '2020-12-31': {'1. open': '121.0100', '2. high':
'138.7890', '3. low': '120.0100', '4. close': '132.6900', '5. volume': '2319687808'}}},
{'Meta Data': {'1. Information': 'Monthly Prices (open, high, low, close) and Volumes', '2.
Symbol': 'ZM', '3. Last Refreshed': '2021-01-29', '4. Time Zone': 'US/Eastern'}, 'Monthly Time
Series': {'2021-01-29': {'1. open': '340.4000', '2. high': '404.4400', '3. low': '331.1000', '4.
close': '372.0700', '5. volume': '121350349'}, '2020-12-31': {'1. open': '434.7200', '2. high':
'434.9900', '3. low': '336.1000', '4. close': '337.3200', '5. volume': '150168985'}}}]
I run the following which gives me the dataframe I want except for the the ticker:我运行以下命令,得到我想要的 dataframe 除了代码:
df = [pd.DataFrame.from_dict(i['Monthly Time Series'], orient= 'index').sort_index(axis=1) for i in json]
Output: Output:
[ 1. open 2. high 3. low 4. close 5. volume
2021-01-29 133.5200 145.0900 126.3820 131.9600 2239366098
2020-12-31 121.0100 138.7890 120.0100 132.6900 2319687808
],
1. open 2. high 3. low 4. close 5. volume
2021-01-29 340.4000 404.4400 331.1000 372.0700 121350349
2020-12-31 434.7200 434.9900 336.1000 337.3200 150168985]
What I want is to pull the value from '2.我想要的是从'2中提取值。 Symbol' from the json and append the respective ticker symbol to the corresponding data like so:符号'从 json 和 append 各自的股票代码到相应的数据,如下所示:
[ 1. open 2. high 3. low 4. close 5. volume ticker
2021-01-29 133.5200 145.0900 126.3820 131.9600 2239366098 AAPL
2020-12-31 121.0100 138.7890 120.0100 132.6900 2319687808 AAPL
],
1. open 2. high 3. low 4. close 5. volume ticker
2021-01-29 340.4000 404.4400 331.1000 372.0700 121350349 ZM
2020-12-31 434.7200 434.9900 336.1000 337.3200 150168985 ZM
] ]
Update:更新:
Single loop one line execution单循环一行执行
df = [ (pd.DataFrame.from_dict(i['Monthly Time Series'] , orient= 'index').sort_index(axis=1).assign(ticker=i['Meta Data']['2.Symbol'])) for i in json]
json data: json 数据:
json =[{
'Meta Data': {
'1. Information': 'Monthly Prices (open, high, low, close) and Volumes','2.Symbol': 'AAPL', '3. Last Refreshed': '2021-01-29', '4. Time Zone': 'US/Eastern'},
'Monthly Time Series': {
'2020-01-29': {
'1. open': '133.5200', '2. high': '145.0900','3. low': '126.3820', '4. close': '131.9600', '5. volume': '2239366098'
},
'2020-01-30': {'1. open': '121.0100', '2. high': '138.7890', '3. low': '120.0100',
'4. close': '132.6900', '5. volume': '2319687808'}
}
},
{
'Meta Data': {
'1. Information': 'Monthly Prices (open, high, low, close) and Volumes','2.Symbol': 'ZM', '3. Last Refreshed': '2021-01-01', '4. Time Zone': 'US/Eastern'},
'Monthly Time Series': {
'2020-02-02': {'1. open': '133.5200', '2. high': '145.0900','3. low': '126.3820',
'4. close' : '131.9600', '5. volume': '2239366098'
},
'2020-02-31':
{
'1. open': '121.0100', '2. high': '138.7890', '3. low': '120.0100','4. close' : '132.6900', '5. volume': '2319687808'}
}
}]
making use of assign for adding a new column利用assign添加新列
addTimeSeries = lambda i : pd.DataFrame.from_dict(i['Monthly Time Series'] , orient= 'index').sort_index(axis=1)
addVal = lambda i: addTimeSeries(i).assign(ticker=i['Meta Data']['2.Symbol'])
df = [ addVal(i) for i in json]
output: output:
[ 1. open 2. high 3. low 4. close 5. volume ticker
2020-01-29 133.5200 145.0900 126.3820 131.9600 2239366098 AAPL
2020-01-30 121.0100 138.7890 120.0100 132.6900 2319687808 AAPL,
1. open 2. high 3. low 4. close 5. volume ticker
2020-02-02 133.5200 145.0900 126.3820 131.9600 2239366098 ZM
2020-02-31 121.0100 138.7890 120.0100 132.6900 2319687808 ZM]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.