SoFunction
Updated on 2024-11-20

Python object-oriented implementation of data analysis examples in detail

case (law)

A company, which has 2 data files, now needs to analyze them and process them to calculate the daily sales and present them in the form of bar charts.

 

demand analysis

Implementation steps

  • Design a class that can accomplish the encapsulation of data
  • Design an abstract class that defines the functionality associated with file reading and use subclasses to implement the specific functionality
  • Reading files and producing data objects
  • Perform logical calculations of data requirements (calculate sales for each day)
  • Graphing through PyEcharts

coding

Example 1

"""
Classes for data definitions
"""
 
class Record:
    def __init__(self,data,order_id,money,province):
        =data
        self.order_id=order_id
        =money
        =province
    def __str__(self):
        return f"{},{self.order_id},{},{}"

Example 2

"""
Class definitions related to documents
"""
import json
 
from data_define import Record
 
class FileReader:
    def read_data(self):
        # Read the file data, read each piece of data are converted to Record objects, they are encapsulated in a list can return
        pass
 
class TextFileReader(FileReader):
    def __init__(self,path):
        =path
    # Rewrite (implement abstract methods) of the parent class
    def read_data(self):
        f=open(,"r",encoding="utf-8")
        record_list=[]
        for line in ():
            line=()# Eliminate "\n" from each line of data read.
            data_list=(",")
            record=Record(data_list[0],data_list[1],int(data_list[2]),data_list[3])
            record_list.append(record)
        ()
        return record_list
 
 
class JsonFileReader(FileReader):
    def __init__(self,path):
        =path
    # Rewrite (implement abstract methods) of the parent class
    def read_data(self):
        f=open(,"r",encoding="utf-8")
        record_list=[]
        for line in ():
            data_dict=(line)
            record=Record(data_dict["date"],data_dict["order_id"],int(data_dict["money"]),data_dict["province"])
            record_list.append(record)
        ()
        return record_list
 
 
if __name__ == '__main__':
    text_file_reader=TextFileReader("D:/January 2011 Sales Data.txt")
    list1=text_file_reader.read_data()
    for l in list1:
        print(l)
    print("========================================================================")
    json_file_reader=JsonFileReader("D:/February 2011 Sales Data")
    list2=json_file_reader.read_data()
    for l in list2:
        print(l)

Example 3

from  import Bar
from  import *
from  import *
 
from file_define import FileReader,TextFileReader,JsonFileReader
from data_define import Record
 
text_file_reader=TextFileReader("D:/January 2011 Sales Data.txt")
json_file_reader=JsonFileReader("D:/February 2011 Sales Data")
 
jan_data=text_file_reader.read_data()
feb_data=json_file_reader.read_data()
 
all_data:list[Record]=jan_data+feb_data
 
# Starting to do the data calculations
data_dict={}
for record in all_data:
    if  in data_dict.keys():
        data_dict[]+=
    else:
        data_dict[]=
 
 
#Visualization
bar = Bar(init_opts=InitOpts(theme=))
bar.add_xaxis(list(data_dict.keys()))
bar.add_yaxis("Sales",list(data_dict.values()),label_opts=LabelOpts(is_show=False))
bar.set_global_opts(
    title_opts=TitleOpts(title="Daily sales")
)
("DailySalesBarChart.html.")

 

visualization

data set

Link:/s/1P3n-gvooVvmHEPak-xmkKg

Extract code: hxvn

To this article on the Python object-oriented implementation of data analysis of the detailed examples of the article is introduced to this, more relevant Python data analysis content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!