2016-02-26 72 views
0

我正在使用熊貓來創建一個數據框,它都很好,但我有兩列,其中有字典。如何拆分這些列以提取價格價值和股權價值。Python熊貓獲得字典在列中的價值

   AgainstSidePrices      ForSidePrices 
0 {u'_Price': 4.8, u'_Stake': 160.69} {u'_Price': 4.6, u'_Stake': 21.44} 
1  {u'_Price': 4.8, u'_Stake': 5.69}  {u'_Price': 4.7, u'_Stake': 4.0} 
2  {u'_Price': 5.0, u'_Stake': 22.32} {u'_Price': 4.9, u'_Stake': 15.34} 
3  {u'_Price': 5.6, u'_Stake': 15.18} {u'_Price': 5.4, u'_Stake': 14.82} 
4  {u'_Price': 9.6, u'_Stake': 4.22} {u'_Price': 9.4, u'_Stake': 6.71} 
5  {u'_Price': 12.5, u'_Stake': 4.0} {u'_Price': 11.5, u'_Stake': 12.35} 
6  {u'_Price': 950.0, u'_Stake': 2.0} {u'_Price': 128.0, u'_Stake': 2.25} 
7         NaN         NaN 
8  {u'_Price': 4.8, u'_Stake': 4.72} {u'_Price': 4.6, u'_Stake': 9.32} 
9  {u'_Price': 4.9, u'_Stake': 2.0} {u'_Price': 4.7, u'_Stake': 3.92} 

我有一個解決方案,但是當有喜歡的NaN線出現問題7

table['price'] = table['AgainstSidePrices'].apply(lambda x: x.get('_Price')) 

你能幫助我嗎?

+2

所以你只是想忽略'NaN'? 'table.loc [table ['AgainstSidePrices']。notnull(),'AgainstSidePrices']。apply(lambda x:x.get('_ Price'))' – EdChum

+0

謝謝。這一切都奏效了。 –

回答

1

根據你所需要的,無論是將其應用到非空條目:

table.AgainstSidePrices[table.AgainstSideProces.notnull()].apply(...) 

或改變apply函數來處理這個問題:

... apply(lambda x: <something> if x is None else x.get('_Price')) 

注意答案的尺寸是不同的:第一個只適用於相關的行,第二個適用於所有行。

+0

'notnull'比'〜isnull'更具可讀性IMO – EdChum

+0

@EdChum非常感謝。將更新。 –