Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

df[['StudentName', 'E1']].repr() while executing the above line I'm getting the…

  1. User

    df[['StudentName', 'E1']].repr() while executing the above line I'm getting the following output

    ',StudentName,E1\n0,student1,92\n1,student2,74\n2,student3,83\n3,student4,100\n4,student5,77\n5,student6,100\n6,student7,66\n7,student8,97\n8,student9,95\n9,student10,78'

    the following is the expected output

    expected_output = """,StudentName,E1 0,student1,92 1,student2,74 2,student3,83 3,student4,100 4,student5,77 5,student6,100 6,student7,66 7,student8,97 8,student9,95 9,student10,78"""

    following is my code

    class DataFrame: def init(self, data: Optional[Any] = None, columns: Optional[List[str]] = None): if not data and not columns: self.data = {} self.columns = [] elif columns: self.columns = columns self.data = {col: ListV2([]) for col in columns} else: raise ValueError("Columns cannot be empty if data is provided")

        if data:
            if isinstance(data, dict):
                for col, values in data.items():
                    if col not in self.columns:
                        self.columns.append(col)
                    self.data[col].lst = values
            elif isinstance(data, list):
                if not columns:
                    raise ValueError("Columns must be provided when data is a list")
                for row in data:
                    for col_name, value in zip(self.columns, row):
                        self.data[col_name].append(value)
            else:
                raise ValueError("Invalid data type. Must be either list or dict.")
        
        self.index = {}
    
    def set_index(self, index: str):
        if index in self.data.keys():
            col = self.data.pop(index)
            self.index = {name: idx for idx, name in enumerate(col)}
    
    def __setitem__(self, col_name: str, values: List[Any]):
        if col_name not in self.columns:
            self.columns.append(col_name)
            self.data[col_name] = ListV2([])
        self.data[col_name] = values
    
    def __getitem__(self, col_name: Any) -> List[Any]:
        if isinstance(col_name, list):
            return self._get_rows(col_name)
        else:
            return self.data[col_name]
        
    def _get_rows(self, col_names: List[str]) -> str:
        cols = [("",) + tuple(col_names)]
        for idx, row in enumerate(zip(*(self.__getitem__(name) for name in col_names))):
            cols.append((idx,) + row)
        return "\n".join([",".join([str(el) for el in row]) for row in cols[:1]] +
                         [",".join([str(el) for el in row]) for row in cols[1:]])
    
    def loc(self, row_name: str) -> Dict[str, Any]:
        idx = self.index[row_name]
        return {col: self.data[col][idx] for col in self.columns}
    
    def iteritems(self):
        return self.data.items()
    
    def iterrows(self):
        for name, idx in self.index.items():
            row = self.loc(idx)
            yield name, row
    
    def astype(self, dtype: type, col_name: Optional[str] = None):
        if col_name:
            self.data[col_name] = ListV2([dtype(x) for x in self.data[col_name]])
        else:
            for col in self.columns:
                self.data[col] = ListV2([dtype(x) for x in self.data[col]])
    
    def drop(self, col_name: str):
        if col_name in self.columns:
            self.columns.remove(col_name)
            self.data.pop(col_name)
    
    def mean(self) -> Dict[str, float]:
        result = {}
        for col in self.columns:
            total = sum(self.data[col])
            mean = total / len(self.data[col])
            result[col] = mean
        return result
    
    def __repr__(self):
        table = [("",) + self.columns]
        for idx, row in enumerate(zip(*self.data.values())):
            table.append([idx] + list(row))
        return "\n".join([",".join([str(el) for el in row]) for row in table])
    

    above is my code fix the code accordingly

  2. Assistant

    I believe the issue is with the _get_rows method in your DataFrame class. It looks like you're trying to return a string representing the selected columns but you're not considering the header line in the output. Here's an updated implementation of the _get_rows method that should fix this issue:

    def _get_rows(self, col_names: List[str]) -> str:
        cols = [("",) + tuple(col_names)]
        for idx, row in enumerate(zip(*(self.__getitem__(name) for name in col_names))):
            cols.append((idx,) + row)
        return "\n".join([",".join(map(str, row)) for row in cols])
    

    With this update, using df[['StudentName', 'E1']].__repr__() should give you the expected output.