Pandas DataFrame中的字典,如何拆分列

时间:2016-01-04 18:47:32

标签: python pandas

我有一个DataFrame,其中包含一个column('Vals'),即dictionaryDataFrame或多或少看起来像这样:

In[215]: fff
Out[213]: 
                                                Vals
0    {u'TradeId': u'JP32767', u'TradeSourceNam...
1    {u'TradeId': u'UUJ2X16', u'TradeSourceNam...
2    {u'TradeId': u'JJ35A12', u'TradeSourceNam...

在查看个人row时,dictionary看起来像这样:

In[220]: fff['Vals'][100]
Out[218]: 
{u'BrdsTraderBookCode': u'dffH',
 u'Measures': [{u'AssetName': u'Ie0',
   u'DefinitionId': u'6dbb',
   u'MeasureValues': [{u'Amount': -18.64}],
   u'ReportingCurrency': u'USD',
   u'ValuationId': u'669bb'}],
 u'SnapshotId': 12739,
 u'TradeId': u'17304M',
 u'TradeLegId': u'31827',
 u'TradeSourceName': u'xxxeee',
 u'TradeVersion': 1}

如何拆分columns并创建新的DataFrame,以便我获得一列TradeId而另一列MeasureValues

2 个答案:

答案 0 :(得分:1)

以下是获取TradeIdMeasureValues的方法(使用上面两倍的示例行来说明迭代):

new_df = pd.DataFrame()
for id, data in fff.iterrows():
    d = {'TradeId': data.ix[0]['TradeId']}
    d.update(data.ix[0]['Measures'][0]['MeasureValues'][0])
    new_df = pd.concat([new_df, pd.DataFrame.from_dict(d, orient='index').T])

  Amount TradeId
0 -18.64  17304M
0 -18.64  17304M

答案 1 :(得分:1)

试试这个:

l=[]

for idx, row in df['Vals'].iteritems():
    temp_df = pd.DataFrame(row['Measures'][0]['MeasureValues'])
    temp_df['TradeId'] =  row['TradeId']
    l.append(temp_df)

pd.concat(l,axis=0)