python - 将包含不同长度列表的字典转换为 2 列数据框
问题描述
我有一本像这样的字典
my_dict = {0:[1,2],
1:[1, 5, 100,120],
2:[1, 89, 90, 1625, 98, 0, 10]}
我想将它转换为只有两列这样的数据框。
col1 col2
0 [1, 2]
1 [1, 5, 100,120]
2 [1, 89, 90, 1625, 98, 0, 10]
第二列包括数字列表。有什么建议么?
解决方案
#Input dictionary
my_dict = {0:[1,2],
1:[1, 5, 100,120],
2:[1, 89, 90, 1625, 98, 0, 10]}
#Convert dictionary to dataframe
df = pd.DataFrame(my_dict.items(),columns=["col1","col2"])
print(df)
输出:
col1 col2
0 0 [1, 2]
1 1 [1, 5, 100, 120]
2 2 [1, 89, 90, 1625, 98, 0, 10]
推荐阅读
- c# - 通过 UIElement.TranslatePoint 计算子元素中心偏离正确值
- python - python3在十六进制中的奇怪行为
- reactjs - 有一个带有路由和路由的反应应用程序在刷新时出错。可能是什么原因?
- php - 将 URL 向上重写一个文件夹
- c# - 如果存在于 DataGridView 单元格上
- django - Django:UpdateView 如何更改 ModelForm 中排除的字段的值?
- python - 为什么 Python Flask-User with MongoDB 会抛出与非 MongoDB 版本相关的错误?
- java - 从 JDBC 模板获取连接时出错
- android - 实施 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' 不起作用
- git - checkout+merge 和 pull 的区别