-1

How to create objects from pandas dataframe?

I have a class named people and i want to create objects based on column dataframe names (the column has people name too) how can be seen below:

What I have:

Class People:
   def__init__(self):
       pass

Pandas Dataframe

People  Age  Profession
Jorge   29   Student
Nathan  36   Enginer

What I want (automatically from dataframe)

Jorge = People()
Nathan = People()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Something like [Convert a Pandas DataFrame into a list of objects](https://stackoverflow.com/q/53192602/15497888) or something else? – Henry Ecker Sep 17 '21 at 17:43
  • The above comment already had a link to a working solution. But what's more important is you should learn the basics of class and try coming up with your own solution, then post any error here to ask for help. Try this link https://www.w3schools.com/python/python_classes.asp – Huy Sep 18 '21 at 06:10

1 Answers1

0

That's what I can propose

Class People:
   def__init__(self, name, age, profession):
       self.name = name
       self.age = age
       self.profession = profession
       
def from_df_to_obj(dataframe):
    people = []
    for i in dataframe.index:
        people.append(People(dataframe["name"][i]), People(dataframe["age"][i]),  People(dataframe["profession"][i]))
    return people
Huy
  • 794
  • 6
  • 10
Rukamakama
  • 780
  • 1
  • 8
  • 16