0

The code runs without error, but the right data is not populating into the resulting dataframe.

I've tried with and without the index and neither seem to work. I looked into dtypes but it looks like they match on the fields I'm using as the index. I noted that the indicator is saying left_only, making me think the merge is not actually bringing anything over. It clearly must not be, because fields that are not null in the right df are showing null in the resulting dataframe.

    df = df[(df['A'].notna())]
    group = df.groupby(['A', 'B', 'Period', 'D'])
    df2 = group['Monthly_Need'].sum()
    df2 = df2.reset_index()
    df = df.set_index(['A', 'B', 'Period', 'D'])
    df2 = df2.set_index(['A', 'B', 'Period', 'D'])
    df = df.merge(df2, how='left', left_index=True, right_index=True, indicator=True)
    df = df.reset_index()

  • The may have been answered already from this post https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right?rq=1 – fwench_toast Jun 07 '19 at 18:29
  • Based on your code, you may not need to even use a merge to get what you want. Do you mind posting an example df and what your end results should look like? – Ben Pap Jun 07 '19 at 18:59
  • @fwench_toast Wrong language. – cs95 Jun 07 '19 at 19:03
  • I'm trying to pull in the Monthly_Need into the new dataframe but it seems to not be matching. – Question_Mark Jun 08 '19 at 18:34
  • ok, so it turns out my code worked fine. one of the keys didn't match when they should have and had to be updated. after that, it worked fine. – Question_Mark Jun 12 '19 at 17:46

0 Answers0