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

后端 未结 5 1033
野的像风
野的像风 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:04

    Paul's original answer was very nice but not correct in general, because there is no assurance that the ordering of columns is the same on the left-hand side and the right-hand side of the last line. (In fact, it does not seem to work on the test data in the question, instead erroneously switching the height and weight columns.)

    We can fix this by ensuring that the list of dict keys on the LHS is sorted. This works because the apply on the RHS automatically sorts by the index, which in this case is the list of column names.

    def CustomParser(data):
      import json
      j1 = json.loads(data)
      return j1
    
    df = pandas.read_csv(f1, converters={'stats':CustomParser},header=0)
    df[sorted(df['stats'][0].keys())] = df['stats'].apply(pandas.Series)
    

提交回复
热议问题