如何将json字符串的pandas列转换为新的数据帧

时间:2016-09-28 13:06:56

标签: json csv pandas

我已经获得了一个csv,其中一列数据是json字符串,加载到pandas时看起来如下所示:

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

将此列转换为基于各种键的新数据帧或新列的最pythonic方法是什么?

1 个答案:

答案 0 :(得分: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