Home > database >  Getting Records from A DataFrame, one by one
Getting Records from A DataFrame, one by one

Time:08-08

I'm implementing an API in order to get a data from a database and return the data according to some conditions.

I managed to create a dataframe by using Pandas. Now, my task is to implement a function that returns the records of the dataframe, one by one, like an iterator. Meaning, each time the user calls this method getRecord(self), he gets the next record.

I'm having trouble with implementing this method and I'd really like to get some help. I looked for a ways to do it by using function of Pandas and couldn't find a thing. I also thought about implement the function __iter__ and __next__ in my class but it didn't work.

CodePudding user response:

Wondering if you are looking something like this -

class DfIter:

def __init__(self):
    self.df: pd.DataFrame = pd.DataFrame({"a": [1,2,3,4,5], "b": [2,4,8,9,5], "c": [3,4,5,6,7]})
    self.state = 0

def getRecord(self):
    
    while self.state < self.df.shape[0]:
        _data = self.df.iloc[self.state] 
        self.state  = 1
        return _data
        
    else: 
        raise IndexError("No more datapoints to return")

iter_obj = DfIter()

iter_obj.getRecord()

CodePudding user response:

Perhaps, you are looking for something as follows -

class LazyPandas:

    def __init__(self, df):
        self.df = df
        self.max = df.shape[0]

    def __next__(self):
        if self.n <= self.max:
            result = self.df.iloc[self.n] 
            self.n  = 1
            return result
        else:
            raise StopIteration        

    def __iter__(self):
        self.n = 0
        return self


  
import pandas as pd
df = pd.read_csv("test.csv")

lazy_df = LazyPandas(df)
i = iter(lazy_df)
print(next(i))
print(next(i))
print(next(i))
print(next(i))

  • Related