简体   繁体   English

遍历嵌套字典以创建 Dataframe 并添加新列值

[英]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.

相关问题 遍历 pandas 数据框中的行并匹配列表字典中的值以创建新列 - Iterate through rows in pandas dataframe and match values in a dictionary of lists to create a new column Python 嵌套字典迭代并创建新字典 - Python nested dictionary iterate and create new dictionary Pandas 遍历一个数据帧,将行值和列值连接到一个关于特定列值的新数据帧中 - Pandas-iterate through a dataframe concatenating row values and column values into a new dataframe with respect to a specific column value 遍历嵌套字典 - Iterate through nested dictionary 遍历 pandas dataframe 的列并为循环中的每个选定列创建一个新的 dataframe - Iterate through columns of pandas dataframe and create a new dataframe for each selected column in a loop 遍历 DataFrame 列名列表,仅将值为整数或浮点数的列名添加到新列表中 - Iterate through a list of DataFrame column names and add only the column names whose values are integers or floats to a new list 遍历DataFrame并使用null值更新列 - Iterate through a DataFrame and update column with value if it is null 迭代字典列表并创建新的字典列表 - Iterate through list of dictionary and create new list of dictionary 遍历字典并从多个字典创建新字典 - Iterate through a dictionary and create new dictionary from multiple dictionaries 如何使用条件遍历列中的每个值并创建具有新值的新列 - How to iterate through each value in a column with conditions and create a new column with new values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM