Reading Json file as Pandas Dataframe error

前端 未结 4 1948
小鲜肉
小鲜肉 2020-12-14 02:33

I have a Json file as follows. It\'s a list of dicts.

[{\"city\": \"ab\", \"trips\": 4, \"date\": \"2014-01-25\", \"value\": 4.7, \"price\": 1.1, \"request_         


        
4条回答
  •  春和景丽
    2020-12-14 03:18

    I think you can use modul json for reading file.json and then DataFrame constructor:

    import pandas as pd
    import json
    
    with open('file.json') as f:
       data = json.load(f)
    print data
    [{u'city': u'ab', u'medium': u'iPhone', u'request_date': u'2014-06-17', u'price': 1.1, u'Weekly_pct': 46.2, u'value': 4.7, u'%price': 15.4, u'avg_price': 5.0, u'date': u'2014-01-25', u'avg_dist': 3.67, u'type': True, u'trips': 4}, {u'city': u'bc', u'medium': u'Android', u'request_date': u'2014-05-05', u'price': 1.0, u'weekly_pct': 50.0, u'value': 5.0, u'%price': 0.0, u'avg_price': 5.0, u'date': u'2014-01-29', u'avg_dist': 8.26, u'type': False, u'trips': 0}]
    
    print pd.DataFrame(data)
    
       %price  Weekly_pct  avg_dist  avg_price city        date   medium  price  \
    0    15.4        46.2      3.67        5.0   ab  2014-01-25   iPhone    1.1   
    1     0.0         NaN      8.26        5.0   bc  2014-01-29  Android    1.0   
    
      request_date  trips   type  value  weekly_pct  
    0   2014-06-17      4   True    4.7         NaN  
    1   2014-05-05      0  False    5.0        50.0  
    

提交回复
热议问题