如何将字典列表转换为熊猫数据框

时间:2019-11-24 14:49:44

标签: python pandas

我从网络抓取中获得了输出,但我不知道如何转换为Pandas Dataframe。

这是我的输出:

[[{'galaxy s10': 1, 'galaxy m30': 1, 'iphone xs max': 1, 'one vision': 1, 'moto g7': 1}], [{'iphone 11': 3}], [], [], [{'iphone 11': 1}, {'iphone 11': 1}]]

我认为此输出是dicts列表的列表。

我想转换成熊猫数据框,如下所示:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这可以解决问题。

const LoadingMyComponent = withLoading(MyComponent);

function MyScreen() {
  // You can exchange data with props or contexts if needed
  return <LoadingMyComponent />;
}

MyScreen.navigationOptions = { /* ... */ };

// MyScreen is never wrapped in a HOC and its navigationOptions are accessible

输出: enter image description here

这部分import pandas as pd from itertools import chain a_list = [[{'galaxy s10': 1, 'galaxy m30': 1, 'iphone xs max': 1, 'one vision': 1, 'moto g7': 1}], [{'iphone 11': 3}], [], [], [{'iphone 11': 1}, {'iphone 11': 1}]] consolidate = [] for l in list(chain(*a_list)): temp = {} for k,(key, value) in enumerate(l.items()) : temp.update({f"Cellphone{k+1}":key, f"Cellphone{k+1}_views":value}) consolidate.append(temp) df = pd.DataFrame.from_dict(consolidate) df 将转换列表列表。