如何将字典项与嵌套列表分开

发布于 2025-01-16 01:47:22 字数 1469 浏览 3 评论 0原文

我陷入了一个项目,我必须从列表中分离所有字典项目并从中创建一个数据框。下面是 json 文件链接。

链接:- https://drive.google.com/file/d /1H76rjDEZweVGzPcziT5Z6zXqzOSmVZQd/view?usp=sharing

我编写了这段代码,将所有列表项转换为字符串。因此我可以将它们分成一个新列表。然而,收集的项目并没有被隐藏到数据框中。我们将非常感谢您的帮助。

     read_cont = []
new_list1 = []
new_list2 = []

     for i in rjson:
for j in rjson[i]:
read_cont.append(rjson[i][j])

      data_filter = read_cont[1]
      for item in data_filter:
      for j in item:
            new_list1.append(item[j])
    
    
      new_list1 =  map(str,new_list1)
    
     for i in new_list1:
        if len(i) > 100:
             new_list2.append(i)
    
        header_names = ["STRIKE PRICE","EXPIRY","underlying", "identifier","OPENINTEREST","changeinOpenInterest","pchangeinOpenInterest", "totalTradedVolume","impliedVolatility","lastPrice","change","pChange", "totalBuyQuantity","totalSellQuantity","bidQty","bidprice","askQty","askPrice","underlyingValue"]
        df = pd.DataFrame(new_list2,columns=header_names)`

它应该看起来像这样............

Columns: [STRIKE PRICE, EXPIRY, underlying, identifier, OPENINTEREST, changeinOpenInterest, pchangeinOpenInterest, totalTradedVolume, impliedVolatility, lastPrice, change, pChange, totalBuyQuantity, totalSellQuantity, bidQty, bidprice, askQty, askPrice, underlyingValue]
Index: []

I am stuck in a project where I have to seperate all Dictionary item from a list and create a dataframe from that. Below is the json file link.

Link:- https://drive.google.com/file/d/1H76rjDEZweVGzPcziT5Z6zXqzOSmVZQd/view?usp=sharing

I had written this code which coverting the all list item into string. hence I am able to seperate them into a new list. However the collected item is not getting coverted into a dataframe. Your help will be highly appriciated.

     read_cont = []
new_list1 = []
new_list2 = []

     for i in rjson:
for j in rjson[i]:
read_cont.append(rjson[i][j])

      data_filter = read_cont[1]
      for item in data_filter:
      for j in item:
            new_list1.append(item[j])
    
    
      new_list1 =  map(str,new_list1)
    
     for i in new_list1:
        if len(i) > 100:
             new_list2.append(i)
    
        header_names = ["STRIKE PRICE","EXPIRY","underlying", "identifier","OPENINTEREST","changeinOpenInterest","pchangeinOpenInterest", "totalTradedVolume","impliedVolatility","lastPrice","change","pChange", "totalBuyQuantity","totalSellQuantity","bidQty","bidprice","askQty","askPrice","underlyingValue"]
        df = pd.DataFrame(new_list2,columns=header_names)`

It should be looking something like this.........

Columns: [STRIKE PRICE, EXPIRY, underlying, identifier, OPENINTEREST, changeinOpenInterest, pchangeinOpenInterest, totalTradedVolume, impliedVolatility, lastPrice, change, pChange, totalBuyQuantity, totalSellQuantity, bidQty, bidprice, askQty, askPrice, underlyingValue]
Index: []

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

import json
import pandas as pd

h = json.load(open('scrap.json'))

mdf = pd.DataFrame()
for i in h['records']['data']:
    for k in i:
        if isinstance(i[k], dict):
            df = pd.DataFrame(i[k], index=[0])
            mdf = pd.concat([mdf, df])
            continue
print(mdf)
import json
import pandas as pd

h = json.load(open('scrap.json'))

mdf = pd.DataFrame()
for i in h['records']['data']:
    for k in i:
        if isinstance(i[k], dict):
            df = pd.DataFrame(i[k], index=[0])
            mdf = pd.concat([mdf, df])
            continue
print(mdf)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文