Parsing a JSON string which was loaded from a CSV using Pandas

后端 未结 5 1078
野的像风
野的像风 2020-11-27 03:24

I am working with CSV files where several of the columns have a simple json object (several key value pairs) while other columns are normal. Here is an example:



        
5条回答
  •  天涯浪人
    2020-11-27 04:08

    I think applying the json.load is a good idea, but from there you can simply directly convert it to dataframe columns instead of writing/loading it again:

    stdf = df['stats'].apply(json.loads)
    pd.DataFrame(stdf.tolist()) # or stdf.apply(pd.Series)
    

    or alternatively in one step:

    df.join(df['stats'].apply(json.loads).apply(pd.Series))
    

提交回复
热议问题