简体   繁体   English

创建列表列表,列表作为查找表

[英]Create a list of lists, List as look up table

I need to create list of lists according to the result from a list as a look up table我需要根据列表的结果创建列表列表作为查找表

List列表

lists=['a','b','c']

Data数据

[
  {'rank': 1, 'keyword_name': 'keyword1', 'volume': None, 'asin': 'a'},
  {'rank': 30, 'keyword_name': 'keyword2', 'volume': None, 'asin': 'b'}
  {'rank': 4, 'keyword_name': 'keyword3', 'volume': 123, 'asin': 'c'}

  {'rank': 5, 'keyword_name': 'keyword3', 'volume': None, 'asin': 'c'}
]

so the new list of lists would be所以新的列表列表将是

// format of result

keyword_name|volume|rank


result = [
    ['keyword1',None,1],
    ['keyword2',None,30],
    ['keyword3',123,4],
    ['keyword3',None,5]
]

My initial code in mind is like below我的初始代码如下所示

results = []
for list in lists:
    for res in data:
        if res['asin'] == list:
           results.append(res)

But we all know it will not work.I need the result to put as data to pandas dataframe, But I have no idea how to achieve it但我们都知道它不起作用。我需要将结果作为数据放入熊猫数据框,但我不知道如何实现它

 import pandas as pd


 df = pd.DataFrame(data=results, columns=lists)

Why don't you simply use pd.DataFrame.from_records(data)你为什么不简单地使用pd.DataFrame.from_records(data)

data = [
  {'rank': 1, 'keyword_name': 'keyword1', 'volume': None, 'asin': 'a'},
  {'rank': 30, 'keyword_name': 'keyword2', 'volume': None, 'asin': 'b'},
  {'rank': 4, 'keyword_name': 'keyword3', 'volume': 123, 'asin': 'c'},
  {'rank': 5, 'keyword_name': 'keyword3', 'volume': None, 'asin': 'c'}
]

df = pd.DataFrame.from_records(data)

甚至简单地说:

df = pd.Dataframe(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM