python - 巧妙地重塑字典以在 python 中制作 pandas 数据框
问题描述
我有一个结构如下的字典:
{'Country_0_0': array([0.00205887, 0.00066778, 0.00018736, 0.00267426, 0.00167584,
0.00325369, 0.00308424, 0.00240303, 0.00232662, 0.0018572 ,
0.00291514, 0.00129124, 0.00213518, 0.00269184, 0.00019298,
0.00275547, 0.00012562, 0.00274757, 0.0014712 , 0.00197154,
0.00225755, 0.00088496, 0.00278242, 0.0011274 , 0.0023305 ,
0.00098155, 0.00210151, 0.00025001, 0.0021463 , 0.00310724,
0.00237708, 0.00026404, 0.00078338, 0.0007101 , 0.00080683,
0.00092139, 0.00045451, 0.00162904, 0.00173966, 0.00230035,
0.00123715, 0.00194307, 0.00115212, 0.0028666 , 0.00036725,
0.00055788, 0.00017001, 0.00297113, 0.00099035, 0.00136376,
0.00035665, 0.00199739, 0.00056606, 0.00170236, 0.00016059,
0.00119921, 0.00161244, 0.00178576]),
'Country_0_1': array([1.38911956e-03, 1.81695317e-03, 1.49397660e-03, 3.36493664e-03,
1.29550916e-03, 2.78850386e-03, 1.46612675e-03, 2.67197959e-03,
2.53572802e-03, 8.43125466e-04, 3.34165244e-03, 3.00652573e-03,
2.16462098e-04, 4.58528408e-04, 2.19940753e-03, 1.32510923e-03,
1.73144250e-03, 1.07272091e-03, 1.19183874e-03, 7.05209922e-04,
1.82857134e-03, 9.19182407e-04, 3.16658232e-03, 3.74238075e-04,
1.05319555e-04, 1.31983491e-03, 1.07057600e-03, 1.31814900e-05,
1.97396178e-03, 2.97744450e-03, 7.69557528e-04, 5.81582604e-04,
3.34432687e-04, 1.25960417e-03, 7.82230675e-04, 3.06447798e-03,
2.54516178e-03, 1.52796734e-03, 1.30200934e-05, 2.81496539e-03,
2.58522073e-04, 2.08142834e-03]), ...
'Country_221_95': array([...])
即,字典的键的形式为:Country_countrynum_productnum。每个国家有 96 个产品,因此字典将由这样的键组成: Country_0_0, Country_0_1, Country_0_2 ...., Country_0_95, Country_1_0, Country_1_1,...Country_1_95,依此类推,直到 Country_221_0, Country_221_1, Country_221_2 。 ...,Country_221_95。每个键都有不同数量的元素(从 40 到 70)。现在我的目标是建立一个熊猫数据框,如下所示:
Country_0 Country_1 Country_2 ... Country_221
Country_0_0[0]. Country_1_0[0] Country_2_0[0] Country_221_0[0]
... ... ... ...
Country_0_0[k00] Country_1_0[k10] Country_2_0[k20] Country_221_0[k22100]
Country_0_1[0] Country_1_1[0] Country_2_1[0] Country_221_1[0]
... ... ... ...
Country_0_1[k01] Country_1_1[k11] Country_2_1[k21] Country_221_1[k2211]
... ... ... ...
Country_0_95[0] Country_1_95[0] Country_2_95[0] Country_1_95[0]
... ... ... ...
Country_0_95[k095] Country_1_95[k195]. Country_2_95[k295] Country_1_95[k22195]
其中 k00, k01, ... 是字典每个键中的元素数。
解决方案
推荐阅读
- r - R 和 RStudio 未检测到已安装的 PostgreSQL(或任何其他)ODBC 驱动程序
- php - 上传文件php时返回false
- mysql - 使用日期和状态字段的mysql订购问题
- android - 可以在样式中完全限定可声明样式的属性(以避免命名冲突)吗?
- sql-server - 带有 CASE & NOT IN 条件的 SQL Where 子句
- django - Django 验证包含在基本模板中的(通讯)表单
- python - 生成给定点 10 公里内的纬度/经度列表
- javascript - 具有_任何可能的属性_的无限深度对象定义为既是又返回另一个无限深度对象的函数。JS
- javascript - 计算传感器未确定长度的运行平均值
- r - 使用 RAdwords 库连接并使用 doAuth 会出错