python - 从列表字典创建数据框
问题描述
我有一个 python 列表字典,我想创建一个多索引数据框
data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data, orient='index')
从上面的代码得到如下输出
0 1 2 3
row_1 3 2 1 0
row_2 a b c d
我想要像下面这样的输出
values
row_1 3
2
1
0
row_2 a
b
c
d
你能帮帮我吗
解决方案
让我们试试stack
df = pd.DataFrame.from_dict(data, orient='index').stack()
Out[298]:
row_1 0 3
1 2
2 1
3 0
row_2 0 a
1 b
2 c
3 d
dtype: object
推荐阅读
- javascript - 尝试使用 axios 遵循 AJAX React 教程
- javascript - d3.js:SVG 条出现在 svg 区域之外
- angular - 如何编写一个与子组件具有相同标签的瘦包装器组件
- jpa - Spring Data Jpa 中的子查询
- android - 如何将构建变体传播到库?
- python - 获取键总数小于或等于 X 的前 5 个值
- django - 如何在 django rest 框架文档中添加参数?
- python - TypeError:切片索引必须是整数或无或具有 __index__ 方法 - Google Sheets API
- python-3.x - 添加 Zip 功能时的列表理解逻辑
- groovy - Groovy 中有没有办法收集列表中的所有对象,其中成员变量与另一个列表中的值匹配?