2016-09-28 25 views
0

我一直在考慮一個CSV文件中的數據的列是一個JSON字符串看起來像當加載到大熊貓如下:如何JSON字符串的大熊貓列轉換成一個新的數據幀

df.registered_office.head() 


3 {u"country": u"United Kingdom", u"region": None, u"postal_code": u"ST6 3HL", u"street_address": u"Martin Leake House Waterloo Road\nCobridge", u"locality": u"Stoke On Trent"} 
5    {u"country": u"England", u"region": None, u"postal_code": u"BS32 4JY", u"street_address": u"Woodlands Grange Woodlands Lane\nBradley Stoke", u"locality": u"Bristol"} 
7  {u"country": u"United Kingdom", u"region": u"Staffordshire", u"postal_code": u"ST6 8JD", u"street_address": u"136 Knypersley Road\nNorton", u"locality": u"Stoke On Trent"} 
8 {u"country": u"United Kingdom", u"region": u"Staffordshire", u"postal_code": u"ST9 9HQ", u"street_address": u"Ashlands\n253 Leek Road, Endon", u"locality": u"Stoke On Trent"} 
9      {u"country": u"United Kingdom", u"region": None, u"postal_code": u"ST1 5TA", u"street_address": u"C/O Kpmg Festival Way\nStoke On Trent", u"locality": None} 
Name: registered_address, dtype: object 

根據各種不同的密鑰,將這一列轉換爲新的數據框或新列的最蟒蛇方式是什麼?

回答

0

我認爲你可以使用DataFrame.from_records

df = pd.DataFrame({'registered_office':[ 
{u"country": u"United Kingdom", u"region": None, u"postal_code": u"ST6 3HL", u"street_address": u"Martin Leake House Waterloo Road\nCobridge", u"locality": u"Stoke On Trent"}, 
{u"country": u"England", u"region": None, u"postal_code": u"BS32 4JY", u"street_address": u"Woodlands Grange Woodlands Lane\nBradley Stoke", u"locality": u"Bristol"}, 
{u"country": u"United Kingdom", u"region": u"Staffordshire", u"postal_code": u"ST6 8JD", u"street_address": u"136 Knypersley Road\nNorton", u"locality": u"Stoke On Trent"}, 
{u"country": u"United Kingdom", u"region": u"Staffordshire", u"postal_code": u"ST9 9HQ", u"street_address": u"Ashlands\n253 Leek Road, Endon", u"locality": u"Stoke On Trent"}, 
{u"country": u"United Kingdom", u"region": None, u"postal_code": u"ST1 5TA", u"street_address": u"C/O Kpmg Festival Way\nStoke On Trent", u"locality": None} 
]}) 
print (pd.DataFrame.from_records(df.registered_office.values.tolist())) 
      country  locality postal_code   region \ 
0 United Kingdom Stoke On Trent  ST6 3HL   None 
1   England   Bristol BS32 4JY   None 
2 United Kingdom Stoke On Trent  ST6 8JD Staffordshire 
3 United Kingdom Stoke On Trent  ST9 9HQ Staffordshire 
4 United Kingdom   None  ST1 5TA   None 

            street_address 
0  Martin Leake House Waterloo Road\nCobridge 
1 Woodlands Grange Woodlands Lane\nBradley Stoke 
2      136 Knypersley Road\nNorton 
3     Ashlands\n253 Leek Road, Endon 
4   C/O Kpmg Festival Way\nStoke On Trent 
+0

謝謝您的迅速反應。它幾乎可以工作,但它將字符串中的單個字符拆分爲列,而不是鍵。 – elksie5000

+0

所以你需要轉置? 'print(pd.DataFrame.from_records(df.registered_office.values.tolist())。T)'? – jezrael

+0

你的輸出是不同的? – jezrael