[英]creating dataframe using list of dictionaries
我有一个字典列表如下
test_lst = [{'col1': 'https://link1.com', 'col2':['data1', 'data2', 'data3']},
{'col1': 'https://link2.com', 'col2':['data3', 'data4', 'data5']},
{'col1': 'https://link3.com', 'col2':['data6', 'data7', 'data8']}]
我想使用这个列表创建一个 dataframe。 dataframe应该如下
col1 col2
0 https://link1.com data1
1 https://link1.com data2
2 https://link1.com data3
3 https://link2.com data3
4 https://link2.com data4
5 https://link2.com data5
6 https://link3.com data6
7 https://link3.com data7
8 https://link3.com data8
但是将test_lst
直接传递给pd.DataFrame
似乎会创建 dataframe 如下
col1 col2
0 https://link.com [data1, data2, data3]
1 https://link.com [data3, data4, data5]
2 https://link.com [data6, data7, data8]
这是我的代码
test_lst = [{'col1': 'https://link1.com', 'col2':['data1', 'data2', 'data3']},
{'col1': 'https://link2.com', 'col2':['data3', 'data4', 'data5']},
{'col1': 'https://link3.com', 'col2':['data6', 'data7', 'data8']}]
df = pd.DataFrame(test_lst)
我究竟做错了什么?
使用DataFrame.explode
是最简单的解决方案:
df = pd.DataFrame(test_lst).explode('col2')
print (df)
col1 col2
0 https://link1.com data1
0 https://link1.com data2
0 https://link1.com data3
1 https://link2.com data3
1 https://link2.com data4
1 https://link2.com data5
2 https://link3.com data6
2 https://link3.com data7
2 https://link3.com data8
或者使用col1
之类的标量创建一个元素列表,然后使用zip_longest
展平,最后向前填充缺失值:
from itertools import zip_longest
test_lst = [{k: v if isinstance(v, list) else [v] for k, v in x.items()} for x in test_lst]
L = [y for x in test_lst for y in zip_longest(*x.values())]
df = pd.DataFrame(L, columns=test_lst[0].keys()).ffill()
print (df)
col1 col2
0 https://link1.com data1
1 https://link1.com data2
2 https://link1.com data3
3 https://link2.com data3
4 https://link2.com data4
5 https://link2.com data5
6 https://link3.com data6
7 https://link3.com data7
8 https://link3.com data8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.