将JSON转换为pd.DataFrame

时间:2019-03-25 13:27:07

标签: python json pandas

我无法将此代码转换为df

我尝试过 df = pd.read_json('australian_user_reviews.json') 并有错误: ValueError: Expected object or value

{'user_id': '76561197970982479', 'user_url': 'http://steamcommunity.com/profiles/76561197970982479', 'reviews': [{'funny': '', 'posted': 'Posted November 5, 2011.', 'last_edited': '', 'item_id': '1250', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Simple yet with great replayability. In my opinion does "zombie" hordes and team work better than left 4 dead plus has a global leveling system. Alot of down to earth "zombie" splattering fun for the whole family. Amazed this sort of FPS is so rare.'}, {'funny': '', 'posted': 'Posted July 15, 2011.', 'last_edited': '', 'item_id': '22200', 'helpful': 'No ratings yet', 'recommend': True, 'review': "It's unique and worth a playthrough."}, {'funny': '', 'posted': 'Posted April 21, 2011.', 'last_edited': '', 'item_id': '43110', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Great atmosphere. The gunplay can be a bit chunky at times but at the end of the day this game is definitely worth it and I hope they do a sequel...so buy the game so I get a sequel!'}]}
{'user_id': 'js41637', 'user_url': 'http://steamcommunity.com/id/js41637', 'reviews': [{'funny': '', 'posted': 'Posted June 24, 2014.', 'last_edited': '', 'item_id': '251610', 'helpful': '15 of 20 people (75%) found this review helpful', 'recommend': True, 'review': 'I know what you think when you see this title "Barbie Dreamhouse Party" but do not be intimidated by it\'s title, this is easily one of my GOTYs. You don\'t get any of that cliche game mechanics that all the latest games have, this is simply good core gameplay. Yes, you can\'t 360 noscope your friends, but what you can do is show them up with your bad ♥♥♥ dance moves and put them to shame as you show them what true fashion and color combinations are.I know this game says for kids but, this is easily for any age range and any age will have a blast playing this.8/8'}, {'funny': '', 'posted': 'Posted September 8, 2013.', 'last_edited': '', 'item_id': '227300', 'helpful': '0 of 1 people (0%) found this review helpful', 'recommend': True, 'review': "For a simple (it's actually not all that simple but it can be!) truck driving Simulator, it is quite a fun and relaxing game. Playing on simple (or easy?) its just the basic WASD keys for driving but (if you want) the game can be much harder and realistic with having to manually change gears, much harder turning, etc. And reversing in this game is a ♥♥♥♥♥, as I imagine it would be with an actual truck. Luckily, you don't have to reverse park it but you get extra points if you do cause it is bloody hard. But this is suprisingly a nice truck driving game and I had a bit of fun with it."}, {'funny': '', 'posted': 'Posted November 29, 2013.', 'last_edited': '', 'item_id': '239030', 'helpful': '1 of 4 people (25%) found this review helpful', 'recommend': True, 'review': 'Very fun little game to play when your bored or as a time passer. Very gud. Do Recommend. pls buy'}]}
{'user_id': 'evcentric', 'user_url': 'http://steamcommunity.com/id/evcentric', 'reviews': [{'funny': '', 'posted': 'Posted February 3.', 'last_edited': '', 'item_id': '248820', 'helpful': 'No ratings yet', 'recommend': True, 'review': "A suitably punishing roguelike platformer.  Winning feels good.  Progressive unlocks mean a good slog ending in failure doesn't feel like a waste."}, {'funny': '', 'posted': 'Posted December 4, 2015.', 'last_edited': 'Last edited December 5, 2015.', 'item_id': '370360', 'helpful': 'No ratings yet', 'recommend': True, 'review': '"Run for fun? What the hell kind of fun is that?"'}, {'funny': '', 'posted': 'Posted November 3, 2014.', 'last_edited': '', 'item_id': '237930', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Elegant integration of gameplay, story, world development and aesthetic.'}, {'funny': '', 'posted': 'Posted October 15, 2014.', 'last_edited': '', 'item_id': '263360', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Random drops and random quests, with stat points.  Animation style reminiscent of the era before the Voodoo card.'}, {'funny': '', 'posted': 'Posted October 15, 2014.', 'last_edited': '', 'item_id': '107200', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Fun balance of tactics and strategy.  Potential for very rewarding battles on smaller maps.  Can become a bit of a grind on larger maps (>200 stars).'}, {'funny': '', 'posted': 'Posted October 15, 2014.', 'last_edited': '', 'item_id': '224500', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Fun world builder, with plenty of option of how you want challenge served to you.  Gnome pathing sometimes frustrating if you expand very very quickly.'}]}
{'user_id': 'doctr', 'user_url': 'http://steamcommunity.com/id/doctr', 'reviews': [{'funny': '', 'posted': 'Posted October 14, 2013.', 'last_edited': '', 'item_id': '250320', 'helpful': '2 of 2 people (100%) found this review helpful', 'recommend': True, 'review': 'This game... is so fun. The fight sequences have been improved from walking dead. It also includes more of a Sam and Max puzzle solving (some of it in the first episode) and walking dead. The game also gets even more better if you have read the Fables comic books, which are without a doubt, very good. The music is also superb and fit the scenarios very well.'}, {'funny': '', 'posted': 'Posted July 28, 2012.', 'last_edited': '', 'item_id': '20920', 'helpful': '1 of 1 people (100%) found this review helpful', 'recommend': True, 'review': "Really Really Really Great Game, very good story, im in chapter 1 atm and i think its great. You get a really early link with characters. No need to play the first game, its nicely wrapped up for you in a five minute video. FYI beware of the sex scenes :P, nudity does happen in the game and it shows you actually having sex, so try not to play with your parents around if your near a point in the story. Also you will need a good rig/comp to play on high. Don't even try running the game on Ultra unless you have mulitiple GPU's (Graphics card) and ram and a good processor. All in all good game :D"}, {'funny': '', 'posted': 'Posted June 2, 2012.', 'last_edited': '', 'item_id': '204100', 'helpful': '1 of 1 people (100%) found this review helpful', 'recommend': True, 'review': 'Just buy it already. Great Story, Great Multiplayer and good fan service. Just awesome game. Just using shootdodge and bullet time makes you feel like a badass. Also, its better if you get the max payne story recapped or replay the first two but its not necessary.'}, {'funny': '', 'posted': 'Posted June 29, 2014.', 'last_edited': '', 'item_id': '224600', 'helpful': '1 of 2 people (50%) found this review helpful', 'recommend': True, 'review': 'It was a great game from what I played, right now I need to find the actual download.'}, {'funny': '', 'posted': 'Posted November 22, 2012.', 'last_edited': '', 'item_id': '207610', 'helpful': 'No ratings yet', 'recommend': True, 'review': "The ending to this game is.... ♥♥♥♥♥♥♥.... Just buy it, you'll be invested, im automatically preordering season two of the walking dead game."}, {'funny': '', 'posted': 'Posted February 23, 2012.', 'last_edited': '', 'item_id': '108710', 'helpful': 'No ratings yet', 'recommend': True, 'review': 'Alan wake is a really good game, the light effects are pretty awesome and this game is so good, it paid back remedy their promotion and conversion fees in the first two days on steam. Its awesome :D. Remedy can still make video games, even if its not Max Payne. It is also very character driven.'}]}
{'user_id': 'maplemage', 'user_url': 'http://steamcommunity.com/id/maplemage', 'reviews': [{'funny': '3 people found this review funny', 'posted': 'Posted April 15, 2014.', 'last_edited': '', 'item_id': '211420', 'helpful': '35 of 43 people (81%) found this review helpful', 'recommend': True, 'review': 'Git gud'}, {'funny': '1 person found this review funny', 'posted': 'Posted December 23, 2013.', 'last_edited': '', 'item_id': '211820', 'helpful': '12 of 16 people (75%) found this review helpful', 'recommend': True, 'review': "It's like Terraria, you play for 9 hours straight, get endgame armour then stop playing until the next update."}, {'funny': '2 people found this review funny', 'posted': 'Posted March 14, 2014.', 'last_edited': '', 'item_id': '730', 'helpful': '5 of 5 people (100%) found this review helpful', 'recommend': True, 'review': 'Hold shift to win, Hold CTRL to lose.'}, {'funny': '', 'posted': 'Posted July 11, 2013.', 'last_edited': '', 'item_id': '204300', 'helpful': 'No ratings yet', 'recommend': True, 'review': "OH YES, THIS GAME IS THE BEST, THEY ADD STUFF LIKE NEW CHARACTERS, AND LIKE A NEW MAP ONCE A YEAR, IT'S SO AWESOME, OH YES, IT'S SO AWESOMENAUTS, YES, YES, I'M GOOD AT THIS, YES, YES, GOOD, I'M GOOD, YES, GOOD, YOU ARE BAD, IM GOOD, YES, TOO GOOD, YES, IM NOT BAD, YES, GOOD."}]}

3 个答案:

答案 0 :(得分:0)

出现您的json问题

使用此json

{
  "bundle_final_price": "$29.66",
  "bundle_url": "http://store.steampowered.com/bundle/1482/?utm_source=SteamDB...",
  "bundle_price": "$32.96",
  "bundle_name": "Two Tribes Complete Pack!",
  "bundle_id": "1482",
  "items": [{"genre": "Casual, Indie", "item_id": "38700", "discounted_price": "$4.99", "item_url": "http://store.steampowered.com/app/38700", "item_name": "Toki Tori"}, {"genre": "Adventure, Casual, Indie", "item_id": "201420", "discounted_price": "$14.99", "item_url": "http://store.steampowered.com/app/201420", "item_name": "Toki Tori 2+"}, {"genre": "Strategy, Indie, Casual", "item_id": "38720", "discounted_price": "$4.99", "item_url": "http://store.steampowered.com/app/38720", "item_name": "RUSH"}, {"genre": "Action, Indie", "item_id": "38740", "discounted_price": "$7.99", "item_url": "http://store.steampowered.com/app/38740", "item_name": "EDGE"}],
  "bundle_discount": "10%"
}

所有值都应该用双引号引起来,否则将为ValueError带来pandas.read_json错误。您可以做的是将单引号替换为双引号。

df2 = pd.read_json('s.json')



  bundle_final_price  ... bundle_discount
0             $29.66  ...             10%
1             $29.66  ...             10%
2             $29.66  ...             10%
3             $29.66  ...             10%

答案 1 :(得分:0)

您的输入不是有效的JSON,它也不是单个JSON表达式,而是多个(每行一个)。字符串应该用双引号引起来,并且对象的整个列表都应使用逗号分隔的[]括弧的数组表达式。

答案 2 :(得分:0)

如果您尝试加载的 JSON 文件格式不正确(此处就是这种情况),您可以使用 ast.literal_eval() 逐行评估。

import ast
import pandas as pd

rows = []
with open('test.json') as f: # Here test.json contains your data
    for line in f.readlines():
        rows.append(ast.literal_eval(line))
        
df = pd.DataFrame(rows)

结果是预期的数据帧:

display(df)

user_id     user_url    reviews
0   76561197970982479   http://steamcommunity.com/profiles/76561197970...   [{'funny': '', 'posted': 'Posted November 5, 2...
1   js41637     http://steamcommunity.com/id/js41637    [{'funny': '', 'posted': 'Posted June 24, 2014...
2   evcentric   http://steamcommunity.com/id/evcentric  [{'funny': '', 'posted': 'Posted February 3.',...
3   doctr   http://steamcommunity.com/id/doctr  [{'funny': '', 'posted': 'Posted October 14, 2...
4   maplemage   http://steamcommunity.com/id/maplemage  [{'funny': '3 people found this review funny',...

那么您可能想将评论分成几列。