0

I have a dataframe (df below) and i want to convert it to an numpy array. the variable mjd takes 1065 unique values and i was trying to get an array for each object_id all the flux corresponding to the different values of Mjd. thanks in advance for the help !

df.head()

Object_id    Mjd     flux         passband   
  615        59750   -544.81         0
  615        59750   -574.80         1
  615        59750    54             2
  615        59750    -44.810303     3
  615        59750    644.810303     4

Output : object_id X flux(mjd)

   array[(615,passband = 1, flux(mjd1), flux(mjd2)..flux(mjd1065)),
    (615,passband = 2, flux(mjd1), flux(mjd2)..flux(mjd1065))
    (615,passband = 3, NaN, flux(mjd2)..NaN)..

    (4512,passband = 4, flux(mjd1), flux(mjd2)..flux(mjd1065))]

if no flux for the corresponding Mjd, NaN is inserted.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Seif
  • 82
  • 1
  • 8
  • Here are two possible links https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array https://stackoverflow.com/questions/49180018/convert-pandas-dataframe-to-numpy-array-which-approach-to-prefer – smitty_werbenjagermanjensen Mar 04 '19 at 15:23
  • thanks, already seen it, but not what i am looking for. **df.to_records** , **df.values** convert to a numpy but not to the structure i need. – Seif Mar 04 '19 at 15:32
  • What type of structure do you need?Do you want all Object_id fields that match be grouped together in a numpy array. – smitty_werbenjagermanjensen Mar 04 '19 at 15:39
  • Yes you summarized all i want in one sentence ! – Seif Mar 04 '19 at 15:41
  • I was about to write some code up for you but I found this and perhaps if you looked through this is may help.https://stackoverflow.com/questions/52621497/pandas-group-by-column-and-transform-the-data-to-numpy-array ... If this doesn't help let me know – smitty_werbenjagermanjensen Mar 04 '19 at 15:59
  • I tried this **n = np.unique(df[:,0]) ; np.array([ df[df[:,0]==i,3] for i in n] )** it gives me what i want but i am not sure about the order. Thanks a lot, i will check the link – Seif Mar 04 '19 at 16:19
  • if you aren't sure about sorting try to call df.sort_values('Column_ Name') – smitty_werbenjagermanjensen Mar 04 '19 at 16:30

0 Answers0