【问题标题】:How to convert a pandas column of json strings into a new dataframe如何将 json 字符串的 pandas 列转换为新的数据框
【发布时间】:2016-09-28 13:06:56
【问题描述】:

我收到了一个 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 方式是什么?

【问题讨论】:

    标签: json csv pandas


    【解决方案1】:

    我觉得你可以用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  
    

    【讨论】:

    • 感谢您的快速回复。它几乎可以工作,但它将字符串中的单个字符分成列,而不是键。
    • 所以你需要转置? print (pd.DataFrame.from_records(df.registered_office.values.tolist()).T) ?
    • 你的输出不一样?
    • 对不起。我失去了网络连接。这是我的回复:对不起,我可能不是很清楚。输出(使用 T 方法)在第一行输出 { { { { { { { { { (每列一个),u u u u u u u u u u (Unicode) on next and then ' ' ' ' ' ' ' ' ' 下一个。
    • 数据是否保密?
    猜你喜欢
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多