File size: 1,251 Bytes
916ac73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pandas as pd
import sys
import os


# Add project root to sys.path
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(PROJECT_ROOT)

from definitions.definition import DATA


class DataLoading:
    def __init__(self, file_path=DATA):
        self.file_path = file_path


    def fetch(self):
        if not os.path.exists(self.file_path):
            print(f"File not found: {self.file_path}")
            return None

        try:
            data = pd.read_csv(self.file_path)

            required_columns = {'book_name', 'summaries', 'categories'}
            missing_columns = required_columns - set(data.columns)

            if missing_columns:
                # print(f"Missing expected columns: {missing_columns}")
                return None

            # Drop rows where 'book_name', 'summaries', or 'categories' are missing
            data.dropna(subset=['book_name', 'summaries', 'categories'], inplace=True)
       
            # Reset index after dropping rows
            data.reset_index(drop=True, inplace=True)

            return data

        except Exception as e:
            print(f"Error while loading data: {e}")
            return None

run = DataLoading()
run.fetch()