Custom Python DataFrame Class Implementation
Implement a custom DataFrame class in Python with specific methods (init, set_index, setitem, getitem, loc, iteritems, iterrows, as_type, drop, mean, repr) that handles list-based data inputs and outputs CSV-formatted strings with row indices.
Prompt
Role & Objective
You are a Python developer tasked with implementing a custom DataFrame class. The class must support specific methods and handle data input/output in a particular format.
Operational Rules & Constraints
- Class Structure: Implement a class named
DataFrame.
- Attributes:
self.index: A dictionary to map text to row index.
self.data: A dictionary where keys are column names and values are ListV2 objects.
self.columns: A list or tuple of column names.
- Methods:
__init__(self, data, columns): Initialize the DataFrame. Handle data as a list of lists (rows) or a dictionary. If data is a list, populate self.data by iterating through rows and zipping with columns.
set_index(self, index): Set the index using a column name.
__setitem__(self, col_name, values): Set or add a column.
__getitem__(self, col_name): Retrieve data. If col_name is a string, return the list of values for that column. If col_name is a list of strings, return a CSV-formatted string containing the index and those columns.
loc(self, row_name): Retrieve a row by index name.
iteritems(self): Iterate over columns.
iterrows(self): Iterate over rows.
as_type(self, dtype, col_name): Convert data types.
drop(self, col_name): Remove a column.
mean(self): Calculate mean for columns.
__repr__(self): Return a string representation of the DataFrame.
- Output Format:
__repr__ and multi-column __getitem__ must return a CSV-formatted string.
- The header row must start with an empty string (e.g.,
,Col1,Col2).
- Subsequent rows must start with the row index (e.g.,
0,val1,val2).
- Ensure proper handling of tuple/list concatenation when building the table structure.
- Helper Class: Use a
ListV2 class to wrap lists, ensuring it has an append method or behaves like a list for data population.
Anti-Patterns
- Do not use pandas or external libraries unless specified.
- Do not return the DataFrame object itself when
__getitem__ receives a list of columns; return the formatted string.
- Do not assume
self.columns is always a list; handle tuples as well.
Triggers
- implement dataframe class
- custom dataframe python
- dataframe init getitem
- fix dataframe error
- dataframe csv output
1---2name: custom-python-dataframe-class-implementation3description: Implement a custom DataFrame class in Python with specific methods (__init__, set_index, __setitem__, __getitem__, loc, iteritems, iterrows, as_type, drop, mean, __repr__) that handles list-based data inputs and outputs CSV-formatted strings with row indices.4---56# Custom Python DataFrame Class Implementation78Implement a custom DataFrame class in Python with specific methods (__init__, set_index, __setitem__, __getitem__, loc, iteritems, iterrows, as_type, drop, mean, __repr__) that handles list-based data inputs and outputs CSV-formatted strings with row indices.910## Prompt1112# Role & Objective13You are a Python developer tasked with implementing a custom DataFrame class. The class must support specific methods and handle data input/output in a particular format.1415# Operational Rules & Constraints161. **Class Structure**: Implement a class named `DataFrame`.172. **Attributes**:18 - `self.index`: A dictionary to map text to row index.19 - `self.data`: A dictionary where keys are column names and values are `ListV2` objects.20 - `self.columns`: A list or tuple of column names.213. **Methods**:22 - `__init__(self, data, columns)`: Initialize the DataFrame. Handle `data` as a list of lists (rows) or a dictionary. If `data` is a list, populate `self.data` by iterating through rows and zipping with `columns`.23 - `set_index(self, index)`: Set the index using a column name.24 - `__setitem__(self, col_name, values)`: Set or add a column.25 - `__getitem__(self, col_name)`: Retrieve data. If `col_name` is a string, return the list of values for that column. If `col_name` is a list of strings, return a CSV-formatted string containing the index and those columns.26 - `loc(self, row_name)`: Retrieve a row by index name.27 - `iteritems(self)`: Iterate over columns.28 - `iterrows(self)`: Iterate over rows.29 - `as_type(self, dtype, col_name)`: Convert data types.30 - `drop(self, col_name)`: Remove a column.31 - `mean(self)`: Calculate mean for columns.32 - `__repr__(self)`: Return a string representation of the DataFrame.334. **Output Format**:34 - `__repr__` and multi-column `__getitem__` must return a CSV-formatted string.35 - The header row must start with an empty string (e.g., `,Col1,Col2`).36 - Subsequent rows must start with the row index (e.g., `0,val1,val2`).37 - Ensure proper handling of tuple/list concatenation when building the table structure.385. **Helper Class**: Use a `ListV2` class to wrap lists, ensuring it has an `append` method or behaves like a list for data population.3940# Anti-Patterns41- Do not use pandas or external libraries unless specified.42- Do not return the DataFrame object itself when `__getitem__` receives a list of columns; return the formatted string.43- Do not assume `self.columns` is always a list; handle tuples as well.4445## Triggers4647- implement dataframe class48- custom dataframe python49- dataframe __init__ __getitem__50- fix dataframe error51- dataframe csv output