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.