如何将字典对象解包到一系列数据帧中



我正在创建一个函数,该函数从ERP系统中获取数据以显示给最终用户。

我想解压缩一个字典对象,并用它们创建一系列Pandas DataFrames。

例如,我有:

troRows
{0: [{'productID': 134336, 'price': '10.0000', 'amount': '1', 'cost': 0}],
1: [{'productID': 142141, 'price': '5.5000', 'amount': '4', 'cost': 0}],
2: [{'productID': 141764, 'price': '5.5000', 'amount': '1', 'cost': 0}],
3: [{'productID': 81661, 'price': '4.5000', 'amount': '1', 'cost': 0}],
4: [{'productID': 146761, 'price': '5.5000', 'amount': '1', 'cost': 0}],
5: [{'productID': 143585, 'price': '5.5900', 'amount': '9', 'cost': 0}],
6: [{'productID': 133018, 'price': '5.0000', 'amount': '1', 'cost': 0}],
7: [{'productID': 146250, 'price': '13.7500', 'amount': '5', 'cost': 0}],
8: [{'productID': 149986, 'price': '5.8900', 'amount': '2', 'cost': 0},
{'productID': 149790, 'price': '4.9900', 'amount': '2', 'cost': 0},
{'productID': 149972, 'price': '5.2900', 'amount': '2', 'cost': 0},
{'productID': 149248, 'price': '2.0000', 'amount': '2', 'cost': 0},
{'productID': 149984, 'price': '4.2000', 'amount': '2', 'cost': 0},

每次函数都需要将x个可能具有不同行数的字典解压缩到一系列DataFrames中。

例如,这一系列的Dictionaries将返回DF0、DF1、DF2、DF3、DF4、DF5、DF6、DF7、DF8。

我可以用打开一个字典

pd.DataFrame(troRows[8])

返回

amount  cost   price  productID
0       2     0  5.8900     149986
1       2     0  4.9900     149790
2       2     0  5.2900     149972
3       2     0  2.0000     149248
4       2     0  4.2000     149984

我如何构建我的代码,以便它为我的所有词典都这样做?

DataFrames字典的解决方案-使用字典理解并将索引值设置为字典的键:

dfs = {k: pd.DataFrame(v) for k, v in troRows.items()}
print (dfs)
{0:   amount  cost    price  productID
0      1     0  10.0000     134336, 1:   amount  cost   price  productID
0      4     0  5.5000     142141, 2:   amount  cost   price  productID
0      1     0  5.5000     141764, 3:   amount  cost   price  productID
0      1     0  4.5000      81661, 4:   amount  cost   price  productID
0      1     0  5.5000     146761, 5:   amount  cost   price  productID
0      9     0  5.5900     143585, 6:   amount  cost   price  productID
0      1     0  5.0000     133018, 7:   amount  cost    price  productID
0      5     0  13.7500     146250, 8:   amount  cost   price  productID
0      2     0  5.8900     149986
1      2     0  4.9900     149790
2      2     0  5.2900     149972
3      2     0  2.0000     149248
4      2     0  4.2000     149984}
print (dfs[8])
amount  cost   price  productID
0      2     0  5.8900     149986
1      2     0  4.9900     149790
2      2     0  5.2900     149972
3      2     0  2.0000     149248
4      2     0  4.2000     149984

一个数据帧的解决方案:

使用带扁平化的list comprehension并将其传递给DataFrame构造函数:

troRows = pd.Series([[{'productID': 134336, 'price': '10.0000', 'amount': '1', 'cost': 0}],
[{'productID': 142141, 'price': '5.5000', 'amount': '4', 'cost': 0}],
[{'productID': 141764, 'price': '5.5000', 'amount': '1', 'cost': 0}],
[{'productID': 81661, 'price': '4.5000', 'amount': '1', 'cost': 0}],
[{'productID': 146761, 'price': '5.5000', 'amount': '1', 'cost': 0}],
[{'productID': 143585, 'price': '5.5900', 'amount': '9', 'cost': 0}],
[{'productID': 133018, 'price': '5.0000', 'amount': '1', 'cost': 0}],
[{'productID': 146250, 'price': '13.7500', 'amount': '5', 'cost': 0}],
[{'productID': 149986, 'price': '5.8900', 'amount': '2', 'cost': 0},
{'productID': 149790, 'price': '4.9900', 'amount': '2', 'cost': 0},
{'productID': 149972, 'price': '5.2900', 'amount': '2', 'cost': 0},
{'productID': 149248, 'price': '2.0000', 'amount': '2', 'cost': 0},
{'productID': 149984, 'price': '4.2000', 'amount': '2', 'cost': 0}]])
df = pd.DataFrame([y for x in troRows for y in x])

另一个扁平化数据的解决方案是使用chain.from_iterable:

from  itertools import chain
df = pd.DataFrame(list(chain.from_iterable(troRows)))

print (df)
amount  cost    price  productID
0       1     0  10.0000     134336
1       4     0   5.5000     142141
2       1     0   5.5000     141764
3       1     0   4.5000      81661
4       1     0   5.5000     146761
5       9     0   5.5900     143585
6       1     0   5.0000     133018
7       5     0  13.7500     146250
8       2     0   5.8900     149986
9       2     0   4.9900     149790
10      2     0   5.2900     149972
11      2     0   2.0000     149248
12      2     0   4.2000     149984

最新更新