我有一本看起来像这样的字典。
new TextFormField(
controller: _userController,
decoration: new InputDecoration(
...
),
)
我想将其转换为类似的数据框。
{ '11457': { 'Client Orders': { 'Z07640C': 422,
'Z04276B': 93,
'Z15421A': 178,
'Z77679': 4},
'Name': 'SANDY',
'Total Orders': 697},
'1191': { 'Client Orders': { 'Z03456B': 101,
'Z10424A': 754,
'Z10453SR': 84,
'Z13454': 658,
'Z13846': 279},
'Name': 'Deep',
'Total Orders': 1876},
'13139': { 'Client Orders': { 'Z32421A': 2,
'Z43487B': 24,
'Z44411LR': 6,
'Z54611SR': 2},
'Name': 'Sran',
'Total Orders': 34}}
我的主键是代码,辅助键是Name。
我希望它在将其导出到csv时看起来像这样。
答案 0 :(得分:2)
将concat
与dict comprehension
一起使用,将MultiIndex
转换为reset_index
列和最后rename
列:
d = { '11457': { 'Client Orders': { 'Z07640C': 422,
'Z04276B': 93,
'Z15421A': 178,
'Z77679': 4},
'Name': 'SANDY',
'Total Orders': 697},
'1191': { 'Client Orders': { 'Z03456B': 101,
'Z10424A': 754,
'Z10453SR': 84,
'Z13454': 658,
'Z13846': 279},
'Name': 'Deep',
'Total Orders': 1876},
'13139': { 'Client Orders': { 'Z32421A': 2,
'Z43487B': 24,
'Z44411LR': 6,
'Z54611SR': 2},
'Name': 'Sran',
'Total Orders': 34}}
d1 = {'level_0':'Code','level_1':'Trading Account'}
df = pd.concat({k: pd.DataFrame(v) for k,v in d.items()}).reset_index().rename(columns=d1)
print (df)
Code Trading Account Client Orders Name Total Orders
0 11457 Z04276B 93 SANDY 697
1 11457 Z07640C 422 SANDY 697
2 11457 Z15421A 178 SANDY 697
3 11457 Z77679 4 SANDY 697
4 1191 Z03456B 101 Deep 1876
5 1191 Z10424A 754 Deep 1876
6 1191 Z10453SR 84 Deep 1876
7 1191 Z13454 658 Deep 1876
8 1191 Z13846 279 Deep 1876
9 13139 Z32421A 2 Sran 34
10 13139 Z43487B 24 Sran 34
11 13139 Z44411LR 6 Sran 34
12 13139 Z54611SR 2 Sran 34
要导出到csv
,请使用to_csv
:
df.to_csv(filename, index=False)
编辑对于删除重复项,需要将它们替换为NaN
s,在to_csv中省略NaN
:
mask = df['Code'].duplicated()
df[['Code', 'Name', 'Total Orders']] = df[['Code', 'Name', 'Total Orders']].mask(mask)
print (df)
Code Trading Account Client Orders Name Total Orders
0 11457 Z04276B 93 SANDY 697.0
1 NaN Z07640C 422 NaN NaN
2 NaN Z15421A 178 NaN NaN
3 NaN Z77679 4 NaN NaN
4 1191 Z03456B 101 Deep 1876.0
5 NaN Z10424A 754 NaN NaN
6 NaN Z10453SR 84 NaN NaN
7 NaN Z13454 658 NaN NaN
8 NaN Z13846 279 NaN NaN
9 13139 Z32421A 2 Sran 34.0
10 NaN Z43487B 24 NaN NaN
11 NaN Z44411LR 6 NaN NaN
12 NaN Z54611SR 2 NaN NaN