how to convert a customized data frame into a list?

Viewed 31

So I wish to convert a customized data frame into a list for exporting and here is my current code building up to it:

import math
cwd = os.path.abspath('') 
files = os.listdir(cwd)  
df = pd.DataFrame()
for file in files:
    if file.endswith('.XLSX'):
        df = df.append(pd.read_excel(file), ignore_index=True)

df = df.where(df.notnull(), None)
array = df.values.tolist()
print(array)

class Item():
    def has_all_properties(self):
        return bool(self.__name and not math.isnan(self.__cost) and self.__gender and self.__prime)
    def clean(self):
        return bool(self.__name and self.__cost <20 and self.__gender == "male" and self.__prime == "yes")
    
    def __init__(self, name, cost, gender, prime):
        self.__name = name
        self.__cost = cost
        self.__gender = gender
        self.__prime = prime

    def __repr__(self):
        return f"Item({self.__name},{self.__cost},{self.__gender},{self.__prime})"

mylist = [Item(*k) for k in array if k[0] and k[1] and k[2] and k[3]]
#print(mylist)

filtered = filter(Item.has_all_properties, mylist)
clean = filter(Item.clean, filtered)
result = list(clean)

#result = result['coln'][idx]

print(result)

The mylist I obtained from the excels data using the class function looks something like this:

mylist =    [Item(book,9.0,male,yes), 
             Item(paint,14.0,male,no), 
             Item(pen,5.0,female,nan), 
             Item(phone case,9.0,nan,no), 
             Item(headphone,40.0,male,yes), 
             Item(coat,nan,male,no),
             Item(comic,15.0,male,yes), 
             Item(nan,15.0,male,no)
             ... and so on]

and after all the data cleaning, the result would look something like this:

  result =  [Item(book,9.0,male,yes), 
             Item(comic,15.0,male,yes),
             ... and so on]

Now I wish to take the items out of the result and make it into an 2d list and get something like this:

list = [(book, 9.0,male,yes),
        (comic, 15.0,male,yes),
         ... and so on]

I am aware that using pandas all the way works, but I wish to explore the class function so I did not do that.

Can you give me some advice, thank you in advance.

2 Answers

Implement __iter__ magic method using yield in your class then call tuple on its' instances, contrived example:

class Item:
    def __init__(self, name, price):
        self.__name = name
        self.__price = price
    def __iter__(self)
        yield self.__name
        yield self.__price
items = [Item("First",10),Item("Second",20)]
tuples = [tuple(i) for i in items]
print(tuples)

output

[('First', 10), ('Second', 20)]

If you want to see just the repr of the object as a string, you have to change your method repr to:

 def __repr__(self):
    return f"({self.__name},{self.__cost},{self.__gender},{self.__prime})"

But if you want to return the object as a tuple inside list, you need to create another method like this:

def tuple(self): 
   return self.__name, self.__cost, self.__gender, self.__prime

And use it with list comprehension to get as a real list of tuples.

t_list = [obj.tuple() for obj in clean]
Related