简体   繁体   English

将 JSON 文件读入 Pandas 进行分析

[英]Reading JSON file into Pandas for analysis

I am getting myself familiar with Classes/OOP in Python and I am practicing on a basic program for tracking finances.我开始熟悉 Python 中的 Classes/OOP,并且正在练习跟踪财务的基本程序。 I have the program to the point where I add entries to a JSON file and save that file away.我有程序,可以将条目添加到 JSON 文件并保存该文件。 Now I want to read in the JSON file into a dataframe and perform some aggregates on it.现在我想将 JSON 文件读入数据帧并对其执行一些聚合。 That's where I am stuck.这就是我被困的地方。 The following fails with:以下失败:

json.decoder.JSONDecodeError: Extra data: line 7 column 2 (char 122)

The JSON file looks like this: JSON 文件如下所示:

{
    "DATE": "2019-02-01 12:57:13.140724",
    "HSA": "600",
    "401K": "90",
    "ROTH": "900",
    "SAVINGS": "1000"
}{
    "DATE": "2019-02-01 12:57:26.995724",
    "HSA": "250",
    "401K": "90",
    "ROTH": "80",
    "SAVINGS": "900"
}

Any ideas?有任何想法吗?

import datetime
import json
import pandas as pd

class BankAccount:

    def __init__(self):
        self.accounts = ['HSA', '401K', 'ROTH', 'SAVINGS']
        self.records = {}
        self.now = datetime.datetime.now()



    def data_entry(self):
        for i in self.accounts:
            x = input('Enter the amount for {}:'.format(i))
            self.records['DATE'] = self.now
            self.records[i] = x


    def display(self):
        return self.records

    def savefile(self):
        with open('finance.json', 'a') as file:
            file.write(json.dumps(self.records, indent=4, sort_keys=True, default=str))
            file.close()

    def analyzedata(self):
        with open('finance.json', 'r') as f:
            obj = json.load(f)
            frame = pd.DataFrame(obj, columns=['401K', 'HSA', 'ROTH', 'SAVINGS', 'DATE'])
            print(frame)





s = BankAccount()
s.data_entry()
s.savefile()
s.analyzedata()

BTW feel free to offer any other suggestions as to why this is a bad way to do it, ie using a Dictionary or whatever it may be.顺便说一句,请随意提供任何其他建议,说明为什么这是一种糟糕的方法,即使用字典或其他任何方法。 Still learning.仍然在学习。 Thanks谢谢

JSON data is represented as one dict, not multiple dicts in a file. JSON 数据表示为一个 dict,而不是一个文件中的多个 dict。 This said I suggest the JSON format be basically a dict that has a key 'data' that holds a list of record dicts.这就是说,我建议 JSON 格式基本上是一个 dict,它具有一个包含record dict 列表的键'data' I also fixed a couple namimg conventions a small things I say to be easier to understand ontop of my comment.我还修复了一些 namimg 约定,这是我在评论中说的更容易理解的小事。

from datetime import datetime
import json
import pandas as pd

class BankAccount:

    def __init__(self, filename='finance.json'):
        self.accounts = ['HSA', '401K', 'ROTH', 'SAVINGS']
        self.records = []
        self.filename = filename

        #load data upon initialization
        self.load_data()

    def load_data(self):
        with open(self.filename, 'r') as file:
            #you may want to do some error checking here
            data = json.load(file)
            self.records = data.get('data', [])

    def data_entry(self):

        #make a new record with current date
        record = {'DATE': datetime.now()}
        for account_name in self.accounts:
            account_data = int(input('Enter the amount for {}:'.format(account_name)))
            record[account_name] = account_data
        self.records.append(record)

        #You made a modification to the records
        #now save it to file
        self.save_data()

    def save_data(self):
        with open(self.filename, 'w') as file:
            #possibly some error checking here as seen fit
            file.write(json.dumps({'data': self.records}, default=str))

    def analyze_edata(self):
        with open(self.filename, 'r') as file:
            df = pd.DataFrame(self.records, columns=self.accounts+['DATE'])
            print(df)

s = BankAccount()
s.data_entry()
s.save_data()
s.analyze_data()

When Ran: *a couple times**当冉: *几次**

Enter the amount for HSA:250
Enter the amount for 401K:90
Enter the amount for ROTH:80
Enter the amount for SAVINGS:900
['HSA', '401K', 'ROTH', 'SAVINGS', 'DATE']
   HSA  401K  ROTH  SAVINGS                        DATE
0  600    90   900     1000  2019-02-01 22:05:06.110471
1  360   100   250      430  2019-02-01 22:06:10.649269
2  250    90    80      900  2019-02-01 22:07:04.176700

finance.json财务.json

{
    "data": [{
        "401K": 90,
        "SAVINGS": 1000,
        "ROTH": 900,
        "HSA": 600,
        "DATE": "2019-02-01 22:05:06.110471"
    }, {
        "401K": 100,
        "SAVINGS": 430,
        "ROTH": 250,
        "HSA": 360,
        "DATE": "2019-02-01 22:06:10.649269"
    }, {
        "401K": 90,
        "ROTH": 80,
        "SAVINGS": 900,
        "HSA": 250,
        "DATE": "2019-02-01 22:07:04.176700"
    }]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM