0

I have an object Gene

class Gene:
    def __init__(self, id, nb_obj, nb_days):
        self.id=id
        self.nb_obj=nb_obj
        self.nb_days=nb_days

And I have a pandas dataframe df

   ID               nb_obj              nb_days
ECGYE                10259            62.965318
NLRTM                 8007            46.550562

How do I load each row of the dataframe to be a Gene object?

m00am
  • 5,910
  • 11
  • 53
  • 69
funkyFunk
  • 69
  • 8
  • Possible duplicate of [pandas, apply with args which are dataframe row entries](https://stackoverflow.com/questions/39814416/pandas-apply-with-args-which-are-dataframe-row-entries) – storaged Jan 16 '18 at 14:51
  • 1
    Welcome to SO. I took the liberty to use code block formatting to refactor your question. You can format code blocks by indenting them by four spaces. Additionally I separated your parameters by a space (which is Python standard). – m00am Jan 16 '18 at 15:24

2 Answers2

1

You can use apply that calls the constructor of Gene:

df.apply(lambda row: Gene(row['id'],row['nb_obj'],row['nb_days']), axis=1)
AndreyF
  • 1,798
  • 1
  • 14
  • 25
0

You can try this :

[Gene(df.ID.loc[i], df.nb_obj.loc[i], df.nb_days.loc[i]) for i in range(df.shape[0])]