熊猫-读取JSON(并添加ID列)

时间:2018-08-20 03:39:54

标签: python json pandas dictionary

不确定这种情况下的最佳标题,但是基本上我有一个JSON输出,我想将其转换为pandas df。

初始JSON如下:

data = [{'Balance': 0.0,
  'Currency': 'USD',
  'Deposit': 0.0,
  'Narration': '',
  'TransactionDate': '2000-01-01T00:00:00Z',
  'TransactionType': 'Opening Balance',
  'ValuePortfolioCurrency': 0.0,
  'Withdrawal': 0.0},
 {'Balance': 15000.0,
  'Currency': 'USD',
  'Deposit': 15000.0,
  'Narration': 'XYZ',
  'TransactionDate': '2010-01-01T00:00:00Z',
  'TransactionType': 'Deposit',
  'ValuePortfolioCurrency': 15000.0,
  'Withdrawal': 0.0},
 {'Balance': 13000.0,
  'Currency': 'USD',
  'Deposit': 0.0,
  'Narration': 'ABC',
  'TransactionDate': '2010-12-01T00:00:00Z',
  'TransactionType': 'Transfer Out',
  'ValuePortfolioCurrency': -2000.0,
  'Withdrawal': -2000.0}]

我可以轻松地将pd.DataFrame(data)放入df。

df:

    Balance Currency    Deposit Narration   TransactionDate     TransactionType     ValuePortfolioCurrency    Withdrawal
0   0.0     USD         0.0               2000-01-01T00:00:00Z  Opening Balance              0.0               0.0
1   15000.0 USD         15000.0  XYZ      2010-01-01T00:00:00Z  Deposit                  15000.0                0.0
2   13000.0 USD         0.0      ABC      2010-12-01T00:00:00Z  Transfer Out            -2000.0              -2000.0

但是,我想将ID从JSON的不同嵌套附加到整个事务块。现在,我可以控制如何添加该ID了。我可以将其添加到列表中,或者使用ID为键的字典吗?

这看起来像下面的片段,使用1234作为ID。

data = ['1234',[{'Balance': 0.0,
  'Currency': 'USD',
  'Deposit': 0.0,
  'Narration': '',
  'TransactionDate': '2000-01-01T00:00:00Z',
  'TransactionType': 'Opening Balance',
  'ValuePortfolioCurrency': 0.0,
  'Withdrawal': 0.0}...]]

或..

data = {'1234':[{'Balance': 0.0,
  'Currency': 'USD',
  'Deposit': 0.0,
  'Narration': '',
  'TransactionDate': '2000-01-01T00:00:00Z',
  'TransactionType': 'Opening Balance',
  'ValuePortfolioCurrency': 0.0,
  'Withdrawal': 0.0}...]}

所以我想要的最终输出是为所有内部事务提供一个具有ID的列,如下所示(请参阅最后一列):

    Balance Currency    Deposit Narration   TransactionDate     TransactionType     ValuePortfolioCurrency    Withdrawal      ID
0   0.0     USD         0.0               2000-01-01T00:00:00Z  Opening Balance              0.0               0.0         1234
1   15000.0 USD         15000.0  XYZ      2010-01-01T00:00:00Z  Deposit                  15000.0                0.0           1234
2   13000.0 USD         0.0      ABC      2010-12-01T00:00:00Z  Transfer Out            -2000.0              -2000.0        1234

我已经用json_normalize尝试了一些方法,但是没有任何效果。希望这一切都有道理,谢谢。

1 个答案:

答案 0 :(得分:1)

这是一个带有虚拟id列表的解决方案。它通过遍历您提供的词典列表并添加名为id的新键来工作:

n = len(data)
ids = ['1234'] * n

for i,j in zip(data, ids):
    i["id"] = j

df = pd.DataFrame(data)

print(df)

  Balance Currency  Deposit Narration       TransactionDate  TransactionType  \
0      0.0      USD      0.0            2000-01-01T00:00:00Z  Opening Balance   
1  15000.0      USD  15000.0       XYZ  2010-01-01T00:00:00Z          Deposit   
2  13000.0      USD      0.0       ABC  2010-12-01T00:00:00Z     Transfer Out   

   ValuePortfolioCurrency  Withdrawal    id  
0                     0.0         0.0  1234  
1                 15000.0         0.0  1234  
2                 -2000.0     -2000.0  1234