avatarThe For Loop

Summarize

An Easy Way to Convert List of Objects to DataFrame and the Reverse in Python

Recently, I have been running into scenarios where I have to either convert a list of objects to pandas DataFrame or DataFrame to a list of objects. So, I came up with a class that can easily help us do this for any object you have. I have given it the name ObjectDF . The best part of using this is that your class can have any number of parameters with any names, and you can use this class to convert between DataFrame and List of objects and visa-versa very easily. The only requirement to use this class is that the class inheriting it should have __init__ function with one parameter for each property of the class (the order of the parameter doesn’t matter).

Class Definition

# For Pandas
import pandas as pd

class ObjectDB:

    # This method can be used to convert a single DataFrame row 
    # to a single Class object
    @classmethod
    def init_pandas(cls, data):
        item = cls(*tuple(data))
        return item
    
    # Convert a DF with multiple rows to list of class objects
    @classmethod
    def init_pandas_list(cls, data):
        data.fillna("", inplace=True)
        items = [cls(*tuple(row)) for index, row in data.iterrows()]
        return items

    # Convert a list of class objects to DF
    @classmethod
    def df(cls, items):
        cls_df = pd.DataFrame([item for item in items])
        cls_df.columns = get_init_params(cls)
        
        return cls_df
   
    # To make class Iterable
    def __iter__(self):
        list_vars = [self.__dict__[var] for var in get_init_params(self)]
        return iter(list_vars)

This class mainly has three classmethods and one method.

  1. init_pandas : This can be used to convert a single DataFrame row to a class object.
  2. init_pandas_list : This can be used to convert a DataFrame to a list of class objects.
  3. df : This can be used to convert a list of class objects to DataFrame. Column names of the DataFrame would be the same as the list of parameters in the derived class’s __init__ method.
  4. __iter__ : This is called internally while using list comprehension in the df method.

Example Usage

Using this class is pretty simple. In the following example, I have created a class Car which inherits from our class ObjectDF.

Note: The __init__ method contains one parameter for each property of the class. If __init__ is not defined in this way, then this class will not work.

# A sample User class which inherits from `ObjectDB` class
class Car(ObjectDB):

    def __init__(self, make, model, year, color, mileage):
        self.make = make
        self.model = model
        self.year = year
        self.color = color
        self.mileage = mileage

The following code creates sample objects for this class.

# Let's first build a few class objects
cars = [
    Car('Toyota', 'Corolla', 2018, 'Blue', 25000),
    Car('Honda', 'Civic', 2020, 'Red', 15000),
    Car('Ford', 'Mustang', 2015, 'Yellow', 40000)
]

To convert these to a data frame, we just need the following line of code.

# With this simple line of code 
cars_df = Car.df(cars)

To do the reverse is also quite simple. This code will return a list of Car objects.

# To do the reverse
cars_objs = Car.init_pandas_list(cars_df)
  • About get_init_params

If you noticed class ObjectDF uses a method get_init_params . This method is used to generate column names for the DataFrame, and it dynamically fetches those names from the __init__ method of the class. Following is the definition of this method, and this is also required for our class to work.

import inspect

# Gives the list of parameters passed in the init method of a class
def get_init_params(class_name):
    method_params = inspect.signature(class_name.__init__).parameters
    param_names = [p for p in method_params if p != 'self']

    return param_names

The complete code for this class and test code are available at this GitHub link.

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Python
Pandas
Pandas Dataframe
Class
Conversion
Recommended from ReadMedium